title: Polish Notation Implementation
body: <p>I want to write a program to calculate output  given arithmetical expression . Like that:</p>
  
  <p>My input is: * + * + 1 2 + 3 4 5 6
  My output should be: 156</p>
  
  <p>I wrote a Java program to do  this using Stack data type. 
  Here is my Java program:</p>
  
  <pre><code>import java.util.Scanner;
  import java.util.Stack;
  
  
  public class Main {
     public  static  void main(String args[]){
         Stack stack =new Stack();
  
         String input;
  
         String trimmedInput[];
         int output;
         int number1,number2;
         int countOfNumber,j;
  
         Scanner scanner = new Scanner(System.in);
  
         System.out.println("put your arithmetical expression. Using Space between ");
         input=scanner.nextLine();
  
         trimmedInput=input.split("\\s+");
  
  //       for(String a:trimmedInput)
  //           System.out.println(a);
  
  
         countOfNumber=trimmedInput.length;
  
  
         for(j=0;j&lt;countOfNumber;j++) {
  
                 if (isNumeric(trimmedInput[j])) {
                     stack.push(trimmedInput[j]);
  
                 }
                  if (trimmedInput[j].equals("+")) {
                     number1 = Integer.parseInt((String) stack.pop()) ;
  
                      number2 = Integer.parseInt((String) stack.pop()) ;
  
                     output = number1 + number2;
  
                     stack.push(output);
  
                 }
  
                 if(trimmedInput[j].equals("-")){
                     number1 = Integer.parseInt((String) stack.pop()) ;
  
                     number2 = Integer.parseInt((String) stack.pop()) ;
  
                     output = number1-number2;
  
                     stack.push(output);
                 }
                  if(trimmedInput[j].equals("*")){
                      number1 = Integer.parseInt((String) stack.pop()) ;
  
                      number2 = Integer.parseInt((String) stack.pop()) ;
  
                     output = number1*number2;
  
                     stack.push(output);
                 }
                 if(trimmedInput[j].equals("/")){
                     number1 = Integer.parseInt((String) stack.pop()) ;
  
                     number2 = Integer.parseInt((String) stack.pop()) ;
  
                     output = number1/number2;
  
                     stack.push(output);
                 }
  
             }
  
         while(!stack.isEmpty())
             System.out.println(stack.pop());
  
  
     }
      public static boolean isNumeric(String str)
      {
          try
          {
              double d = Double.parseDouble(str);
          }
          catch(NumberFormatException nfe)
          {
              return false;
          }
          return true;
      }
  
  }
  </code></pre>
  
  <p>Ok. Here is my problem. If I want to calculate <code>* + * + 1 2 + 3 4 5 6</code> something like that, my compiler gives an error like that:</p>
  
  <pre><code>Exception in thread "main" java.util.EmptyStackException    
  at java.util.Stack.peek(Stack.java:102)     
  at java.util.Stack.pop(Stack.java:84)   
  at Main.main(Main.java:41)  
  </code></pre>
  
  <p>Here is my 41. row at code: </p>
  
  <pre><code>number1 = Integer.parseInt((String) stack.pop()) ;
  </code></pre>
  
  <p>I cannot figured out what is problem in my code. I'm new at Java. Please help me. Thanks a lot :) </p>