Hello, I've been working on this for weeks now and I'm still not getting it. It works fine with two numbers but any more than that and it'll crash or return the wrong value. Please check my code and let me know what I can do to fix it. I can only use stacks and queues. I also have a menu and driver but I don't know if that's needed. My assignment is basically to use scanner to look for input values and operators and then solve it. It's only single digits and I'm supposed to be able to input as many digits and operators as I want and the code should still be able to solve it. Please help!
public class ReversePolishNotation { Queue <String> myQueue; public ReversePolishNotation() { myQueue = new LinkedList <String>(); } public void doMath() { Stack <Integer> myStack = new Stack <Integer>(); Queue <String> sign = new LinkedList <String>(); Scanner console = new Scanner(System.in); int second; int first; while(console.hasNextInt()) { myStack.push(console.nextInt()); } while(console.hasNextLine() && !(console.nextLine()).equals("q")) { sign.add(console.nextLine()); } String operator = sign.remove(); second = myStack.pop(); first = myStack.pop(); do { if(operator.equals("+")) { int result = second+first; myStack.push(result); myQueue.add(first + "+" + second + "=" + result); if(!sign.isEmpty()) { operator = sign.remove(); second = myStack.pop(); first = myStack.pop(); } } else if(operator.equals("-")) { int result = first-second; myStack.push(result); myQueue.add(first + "-" + second + "=" + result); if(!sign.isEmpty()) { operator = sign.remove(); second = myStack.pop(); first = myStack.pop(); } } else if(operator.equals("*")) { int result = second*first; myStack.push(result); myQueue.add(first + "*" + second + "=" + result); if(!sign.isEmpty()) { operator = sign.remove(); second = myStack.pop(); first = myStack.pop(); } } else { int result = first/second; myStack.push(result); myQueue.add(first + "/" + second + "=" + result); if(!sign.isEmpty()) { operator = sign.remove(); second = myStack.pop(); first = myStack.pop(); } } } while(!sign.isEmpty()); } }