import java.util.Stack; public class Dijkstra { public static Integer eval(String[] inp) { Stack operandStk = new Stack<>(); Stack operatorStk = new Stack<>(); String s; for ( int i = 0; i < inp.length; i++ ) { s = inp[i]; System.out.println(">" + s); if ( s.equals("(") ) { // Do nothing... wait for a ")" } else if ( s.equals(")") ) { // Eval one bracketed expression int o2 = operandStk.pop(); int o1 = operandStk.pop(); String op = operatorStk.pop(); int r = operate(op, o1, o2); operandStk.push( r ); // Result is operand for next operation } else if ( s.equals("x") || s.equals("/") || s.equals("+") || s.equals("-") ) operatorStk.push( s ); // Save on operatorStk else operandStk.push(Integer.parseInt(s)); // Save on operandStk System.out.println("operandStk = " + operandStk); System.out.println("operatorStk = " + operatorStk); } return operandStk.pop(); // Return result } public static int operate( String op, int o1, int o2 ) { if ( op.equals("x") ) { System.out.println(">>>" + o1 + " * " + o2); return( o1*o2 ); } else if ( op.equals("/") ) { System.out.println(">>>" + o1 + " / " + o2); return( o1/o2 ); } else if ( op.equals("+") ) { System.out.println(">>>" + o1 + " + " + o2); return( o1+o2 ); } else if ( op.equals("-") ) { System.out.println(">>>" + o1 + " - " + o2); return( o1-o2 ); } else return 0; } }