Search code examples
javaarraysstringstackrpn

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


i am trying to implement a java method which accepts an input in RPN (Reverse Polish Notation) and through use of a stack converts it into an infix notation and calculates it. I have built the stack and a working converter but i am finding problems when it comes to accepting multiple digit numbers (such as 10), my idea to solve this was to enter each separate entity separated by a space, 10+20 would be entered as "10 20 +" but this is resulting in an out of bounds error. Without the section marked below the program works fine for equations such as "12+" (1+2) and more complex ones as long as they involve single digit values. The stack is fully functiopnal too with push and pop methods

   public static void stackRPN(){
    Stack myStack = new Stack();

            Scanner sc = new Scanner(System.in);
            System.out.println("Enter an equation: ");
            String eq = sc.nextLine();
            int len = eq.length();


            for (int i = 0; i < len; i++){
                String car1 = String.valueOf(eq.charAt(i));

                if ("+".equals(car1) || "-".equals(car1) || "/".equals(car1) || /*"car1"*/"x".equals(car1)){   
                String a = myStack.pop();
                String b = myStack.pop();                    

                //This handlws all the 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 does not work

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

Till here

                myStack.push(car1);
                }
            }
               mainMenu(); 
    }

Solution

  • This was fixed using the split method in the string class as so

    public static void stackRPN(){
        Stack myStack = new Stack();
    
                Scanner sc = new Scanner(System.in);
                System.out.print("Enter an equation: ");
                System.out.println();
                String eq = sc.nextLine();
    
                //This Bit splits up the string where it meets a space
    
                String[] eqS = eq.split(" ");
                int len = eqS.length;
    
    
                for (int i = 0; i < len; i++){
                    String car1 = eqS[i];
    
                    if ("+".equals(car1) || "-".equals(car1) || "/".equals(car1) || /*"car1"*/"x".equals(car1)){   
                    String a = myStack.pop();
                    String b = myStack.pop();                    
    
                    //This handlws all the 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(); 
        }