# Reverse Polish Notation Calculator

• May 8th, 2012, 05:55 PM
captainawesome
Reverse Polish Notation Calculator
Here is my code for what i have done using an array stack.
there are no errors but my program doesn't return the final value when I enter "="
Code java:

```import cs2.*; import java.util.Scanner;   public class ArrayStack<T> implements Stack<T> {   private T[] data;   private int topIndex;   @SuppressWarnings("unchecked") public ArrayStack (int initSize) { data = (T[])new Object[initSize]; topIndex = -1; } // Constructor   public ArrayStack () { this(100); } // Constructor   public void push (T item)   // Push the new item onto an ArrayStack { if (topIndex >= data.length-1) throw new NoSpaceAvailableException(); data[++topIndex] = item; } // push     public T pop () // Pop item off top of stack { if (topIndex < 0) throw new EmptyException("stack is empty"); return data[topIndex--]; } // pop   public T top () // Return a copy of top item { if (topIndex < 0) throw new EmptyException("stack is empty"); return data[topIndex]; } // top     public boolean isEmpty () // Return TRUE if no items on stack { return topIndex < 0; } // isEmpty   //public static double execute(String expr)         public static void main(String[] args) { ArrayStack<Double> st = new ArrayStack<>();     Scanner scan = new Scanner(System.in);     Object x = scan.next();         while(scan.hasNext()) { if(scan.hasNextDouble()) { st.push(scan.nextDouble()); } else {   double a, b;       if (x == '+') {a = st.pop(); b = st.pop(); st.push(b + a);}   else if (x == '-') {a = st.pop(); b = st.pop(); st.push(b - a);} else if (x == '*') {a = st.pop(); b = st.pop(); st.push(b * a);}       else if (x == '/') {a = st.pop(); b = st.pop(); st.push(b / a);}     else if (x == '=') {st.pop(); System.out.print("The answer is" + st.pop());   }   } }           } //main   }// class ArrayStack```
• May 8th, 2012, 06:48 PM
Norm
Re: Reverse Polish Notation Calculator
what does it return? Post the print out from the program.