title: Empty Stack Exception for Infix Expression Solver
body: <p>So this program is suppose to take a syntactically correct infix expression that contain integer operands and the four arithmetic operators (+ - * /) from a GUI and display the results. Im having trouble when an expression is entered in parentheses. </p>
  
  <p>For example, this expression</p>
  
  <pre><code>5+3-2 
  </code></pre>
  
  <p>will return 6, but the same expression in parenthesis </p>
  
  <pre><code>(5+3-2)
  </code></pre>
  
  <p>returns a empty stack exception.</p>
  
  <p>I've tried stepping through this as carefully as i could but i'm to know avail. The compiler is pointing the error within the third while loop which would indicate to my best guess that the stack valueStack doesn't have two values at the time but i can't seem to figure out why. Can anybody help me figure out what i'm doing wrong?</p>
  
  <pre><code>   public String infix(String expression)
     {
        expression=expression.replaceAll("[\t\n ]", "");
        String operator = "*/+-";
        int value1, value2;
        char ch;
        StringTokenizer tokenizer = new StringTokenizer(expression, operator, true); 
        Stack&lt;Integer&gt; valueStack = new Stack&lt;Integer&gt;();
        Stack&lt;Character&gt; operatorStack = new Stack&lt;Character&gt;();
  
        while(tokenizer.hasMoreTokens())
        {
           String token = tokenizer.nextToken();
           if(isInteger(token) == true)
              valueStack.push(Integer.parseInt(token));
           else if(token.charAt(0) == '(')
              operatorStack.push(token.charAt(0));
           else if(token.charAt(0) == ')')
              while(operatorStack.peek() != '(')
              {
                 value1 = valueStack.pop();
                 value2 = valueStack.pop();
                 valueStack.push(solver(value1, value2, operatorStack.pop()));
                 operatorStack.pop();
              }
           else if(token.charAt(0) == '+' || token.charAt(0) == '-' || token.charAt(0) == '*' || token.charAt(0) == '/')
           {
              while(!operatorStack.isEmpty() &amp;&amp; precedence(token.charAt(0)) &lt;= precedence(operatorStack.peek()))
              {
                 value1 = valueStack.pop(); 
                 value2 = valueStack.pop(); //empty stack error starts here
                 valueStack.push(solver(value1, value2, operatorStack.pop()));
              }
              operatorStack.push(token.charAt(0));
           }
        }
        while(!operatorStack.isEmpty())
        {
           value1 = valueStack.pop();
           value2 = valueStack.pop();
           ch = operatorStack.pop();
           valueStack.push(solver(value1, value2, ch));
        }
  
  
        String result = Integer.toString(valueStack.pop());
        return result;     
     }  //End of infix
  
     public static boolean isInteger(String s)
     {
        try
        { 
           Integer.parseInt(s);
        } 
        catch(NumberFormatException e)
        {
           return false; 
        } 
        catch(NullPointerException e) 
        {
           return false;
        }
        return true;
     } // end of isInteger
  
     public int solver( int value1, int value2, char operator)
     {
        try
        {
           if(operator == '*')
              return value2 * value1;
           else if(operator == '/')
              return value2 / value1;
           else if(operator == '+')
              return value2 + value1;
           else if(operator == '-')
              return value2 - value1;
           else
              return 0;
        }
        catch(ArithmeticException e )
        {
           JOptionPane.showMessageDialog(null, "Division by Zero");
        }
        return 0;
     } // end of solver
  
     public int precedence(char op)
     {
        if(op == '+' || op == '-')
           return 1;
        else if(op == '*' || op == '/')
           return 2;
        else
           return 0;
     }   // end of precedence
  </code></pre>