Search code examples
stackswitch-statementcasetokenrpn

Reverse Polish Notation Java


I want to make Reverse Polish Notation algorithm, but my code isn't working. Can anyone explain me why ? In my code, I would like to take the characters, until "/+-*"tokens . If this tokens was next , make operation , that contains in case .

import java.util.Stack;
import java.util.StringTokenizer;

import javax.swing.JOptionPane;

public class ONP {

    public static void main(String[] args) {
        Stack<Double> stack = new Stack<Double>();
        double number1;
        double number2;

        String x = JOptionPane.showInputDialog("Tokens");
        StringTokenizer str = new StringTokenizer(x);

        while (str.hasMoreElements()) {
            str.nextElement();
            stack.push((Double) str.nextElement());

            for (int i = 0; i < x.length(); i++) {

                switch (x.charAt(i)) {
                case '+':
                    number1 = stack.pop();
                    number2 = stack.pop();
                    System.out.println(number1 + number2);
                    break;
                case '-':
                    number1 = stack.pop();
                    number2 = stack.pop();
                    System.out.println(number1 - number2);
                    break;
                case '/':
                    number1 = stack.pop();
                    number2 = stack.pop();
                    System.out.println(number1 / number2);
                    break;
                case '*':
                    number1 = stack.pop();
                    number2 = stack.pop();
                    System.out.println(number1 * number2);
                    break;
                }

            }
            System.out.println(stack.pop());
        }
    }

}

I would like to write it without StringBuilder or sth like that.

Input:

2 2 + 2 / 2 /

Output :

(2+2) / 2 / 2

Solution

  • try this

    String s1;  
    
    
    
    while (str.hasMoreElements()) 
    { 
         s1 = str.nextElement();
    
         if(s1.equals("+") || s1.equals("-") || s1.equals("*") || s1.equals("/"))
         {
              switch (s1) 
              {
                    case '+':
                        number1 = stack.pop();
                        number2 = stack.pop();
                        System.out.print(number1 + " + " + number2);
                        break;
                    case '-':
                        number1 = stack.pop();
                        number2 = stack.pop();
                        System.out.print(number1 +" - "+ number2);
                        break;
                    case '/':
                        number1 = stack.pop();
                        number2 = stack.pop();
                        System.out.print(number1 +" / "+ number2);
                        break;
                    case '*':
                        number1 = stack.pop();
                        number2 = stack.pop();
                        System.out.print(number1 +" * "+ number2);
                        break;
                    }
             }
             else
             {
                     stack.push(s1);
             }
    
        }
    

    instead of

     while (str.hasMoreElements()) {
            str.nextElement();
            stack.push((Double) str.nextElement());
    
            for (int i = 0; i < x.length(); i++) {
    
                switch (x.charAt(i)) {
                case '+':
                    number1 = stack.pop();
                    number2 = stack.pop();
                    System.out.println(number1 + number2);
                    break;
                case '-':
                    number1 = stack.pop();
                    number2 = stack.pop();
                    System.out.println(number1 - number2);
                    break;
                case '/':
                    number1 = stack.pop();
                    number2 = stack.pop();
                    System.out.println(number1 / number2);
                    break;
                case '*':
                    number1 = stack.pop();
                    number2 = stack.pop();
                    System.out.println(number1 * number2);
                    break;
                }
    
            }
            System.out.println(stack.pop());
        }