Trouble with parsing and "doing math" with numbers (A simple java calculator) -


i attempting take simple string statement , parse print out simple answer.

so far, cannot figure out why consistently getting wrong answers.

say example - plug in string "2 * 3 * 4 * 5 / 2 / 3 / 2".

the expected answer 10, receive answer of 1.5

can see issue here? assume not case of order of operations (i have not got far yet).

public class testingforexcel {     static string tableholder = "2 * 3 * 4 * 5 / 2 / 3 / 2";     public static void main(string args[]){         string[] fracequationholder = tableholder.split(" ",tableholder.length()); // holds fractions , operator         string operators = "";         double operand;         double operand2;         double answer = 0;          for(int =0; <= (fracequationholder.length-2); i+=2){             operators = fracequationholder[i+1];             operand = double.parsedouble(fracequationholder[i]);             operand2 = double.parsedouble(fracequationholder[i+2]);             if(operators.indexof("+")>=0){                 answer = operand + operand2;             }else if(operators.indexof("-")>=0){                 answer = operand - operand2;             }else if(operators.indexof("*")>=0){                 answer = operand * operand2;             }else if(operators.indexof("/")>=0){                 answer = operand / operand2;             }else                 system.out.print(answer+"");          }         system.out.print(answer+"");       } 

public class testingforexcel {     static string tableholder = "2 * 3 * 4 * 5 / 2 / 3 / 2";     public static void main(string args[]){          string[] fracequationholder = tableholder.split(" ",tableholder.length()); // holds fractions , operator         string operators = "";         double operand;         double operand2;         double answer = double.parsedouble(fracequationholder[0]);          for(int =0; <= (fracequationholder.length-2); i+=2){             operators = fracequationholder[i+1];             operand = double.parsedouble(fracequationholder[i]);             operand2 = double.parsedouble(fracequationholder[i+2]);             if(operators.indexof("+")>=0){                 answer += operand2;             }else if(operators.indexof("-")>=0){                 answer -= operand2;             }else if(operators.indexof("*")>=0){                 answer *=  operand2;             }else if(operators.indexof("/")>=0){                 answer /=   operand2;             }else                 system.out.print(answer+"");          }         system.out.print(answer+"");       } 

you have problems:

1- 1.5 because last operation (3/2) should use (answer += operand + operand2)

2- should use sum of last operation not operands


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -