Click here to Skip to main content
15,906,097 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can i transform this into better code so i can have more then 1 +'s - or * currently i can only use 1 + so i cant do this 4+4+5 it will crash can anyone help me out,also, Things like 2+1 x 3 works but not things lke 3 x 2 + 3 thats really weird aswell this is the piece of code that it is about,
private static int operatorCount(String formule) {
       int resultaat = 0;

       for (int c =0; c<operators.length; c++) {
           if (formule.indexOf(operators[c]) >= 0) {
               resultaat++;
           }
       }
       return resultaat;
   }


What I have tried:

this is my whole code i aswell have a other file but that are just the formulas,

public class FormuleBereken {
    static char[] operators = {'*', '+','-'};

    static Integer bereken(String formule) {

        formule = cleanUp(formule);
        int resultaat = 0;

        for(int i = 0; i< operators.length; i++) {
            int position = formule.indexOf(operators[i]);

            if (position > -1) {
                int start = position-1;
                while ((start>0)&&(Character.isDigit(formule.charAt(start-1)))) {
                    start --;
                }

                String str1 = formule.substring(start, position);
                String str2 = formule.substring(position + 1);
                String subFormule = formule.substring(start);
                System.out.println(str1);

                //ERROR: Want hij geeft een string mee "2+1" en dat kan niet
                int value1 = Integer.parseInt(str1);
                int value2 = Integer.parseInt(str2);
                switch( operators[i]) {
                    case '*': resultaat = value1 * value2; break;
                    case '+': resultaat = value1 + value2; break;
                    case '-': resultaat = value1 - value2; break;
                }

                formule = formule.replace(subFormule, Integer.toString(resultaat));
                if (operatorCount(formule)!=0) {
                    resultaat = bereken(formule);
                }

            }
        }

        return resultaat;
    }

    // EM: deze geeft nog niet  het goede resultaat , want hij telt elke operator  maar een keer. BDV :dubbele for loop?
    private static int operatorCount(String formule) {
        int resultaat = 0;

        for (int c =0; c<operators.length; c++) {
            if (formule.indexOf(operators[c]) >= 0) {
                resultaat++;
            }
        }
        return resultaat;
    }



    private static String cleanUp(String formule) {
        String newFormule = formule.replaceAll(" ", "");

        return newFormule;
    }
}



if anyone could help me out it will be very appreciated Thanks.
Posted
Updated 19-Jun-22 22:41pm
v2

1 solution

That is not the best way to do things. Assuming you are not constrained by the BODMAS rule you can simply parse the string into its separate parts: number operator number operator ...

You can then convert each number to its integral or floating point type and apply the operator that connects each pair. If you do want to follow the rules of BODMAS, then you will need to do multiple passes over the parts of the string to apply all the multiply operations first.
 
Share this answer
 
Comments
Member 15670930 20-Jun-22 5:12am    
I don't know the BODMAS rule , can you show me how to do it without the bodmas rule.
Richard MacCutchan 20-Jun-22 5:16am    
It is just the mathematical rule that states which order the operators should be actioned:
B - parts in brackets ()
O - can't remember
D - divisions
M - multiplications
A - additions
S - subtractions

So if you are using that rule then you must calculate all the multiplications before the additions, and the additions before the subtractions.
Member 15670930 20-Jun-22 5:18am    
I'm not using that rule only + - and x
Richard MacCutchan 20-Jun-22 5:47am    
OK, then you just apply the operators in the order they are presented in the formula string.
Member 15670930 20-Jun-22 5:49am    
i have

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900