java - Can't detect 2 digit numbers when converting from RPN to Infix -


i trying implement java method accepts input in rpn (reverse polish notation) , through use of stack converts infix notation , calculates it. have built stack , working converter finding problems when comes accepting multiple digit numbers (such 10), idea solve enter each separate entity separated space, 10+20 entered "10 20 +" resulting in out of bounds error. without section marked below program works fine equations such "12+" (1+2) , more complex ones long involve single digit values. stack functiopnal push , pop methods

   public static void stackrpn(){     stack mystack = new stack();              scanner sc = new scanner(system.in);             system.out.println("enter equation: ");             string eq = sc.nextline();             int len = eq.length();               (int = 0; < len; i++){                 string car1 = string.valueof(eq.charat(i));                  if ("+".equals(car1) || "-".equals(car1) || "/".equals(car1) || /*"car1"*/"x".equals(car1)){                    string = mystack.pop();                 string b = mystack.pop();                                      //this handlws digits                 double bi = double.parsedouble(b);                 double ai = double.parsedouble(a);                 double finalno = 0;                  switch (car1) {                     case "+":  finalno = bi + ai;                              break;                     case "-":  finalno = bi - ai;                              break;                     case "/":  finalno = bi / ai;                              break;                     case "x":  finalno = bi * ai;                              break;                }                     mystack.push(finalno+"");                   string fineq = b+car1+a;                 system.out.println(fineq + " = " +finalno);                  } else { 

this bit not work

                    while (len < i+1 && eq.charat(i+1) != ' '){                     car1 = car1+eq.charat(i+1);                     i++;                     } 

till here

                mystack.push(car1);                 }             }                mainmenu();      } 

this fixed using split method in string class so

public static void stackrpn(){     stack mystack = new stack();              scanner sc = new scanner(system.in);             system.out.print("enter equation: ");             system.out.println();             string eq = sc.nextline();              //this bit splits string meets space              string[] eqs = eq.split(" ");             int len = eqs.length;               (int = 0; < len; i++){                 string car1 = eqs[i];                  if ("+".equals(car1) || "-".equals(car1) || "/".equals(car1) || /*"car1"*/"x".equals(car1)){                    string = mystack.pop();                 string b = mystack.pop();                                      //this handlws digits                 double bi = double.parsedouble(b);                 double ai = double.parsedouble(a);                 double finalno = 0;                  switch (car1) {                     case "+":  finalno = bi + ai;                              break;                     case "-":  finalno = bi - ai;                              break;                     case "/":  finalno = bi / ai;                              break;                     case "x":  finalno = bi * ai;                              break;                }                      mystack.push(finalno+"");                   string fineq = b+car1+a;                 system.out.println(fineq + " = " +finalno);                  } else {                 mystack.push(car1);                 }             }                mainmenu();      } 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -