title: Parenthesis error check for infix to postfix conversion Java
body: <p>I have been working on a program for infix to postfix conversion and have everything working except that I cannot figure out where to put an error check for a missing left parenthesis. Basically the user inputs a String and the program goes to this class and converts it but I want to make sure they have entered the correct amount of parenthesis. I have tried multiple places but keep coming up with EmptyStackExceptions.</p>
  
  <pre><code>import java.util.*;
  
  public class PostfixConversion {
  
  
     public static boolean precedence(char first, char second)
     {
        int v1 = 0, v2 = 0;
        //find value for first
        if(first == '-' || first == '+'){
           v1 = 1;
        }else if(first == '*' || first == '/'){
           v1 = 2;    
        }//end if
  
        //find value for second
        if(second == '-' || second == '+'){
           v2 = 1;
        }else if(second == '*' || second == '/'){
           v2 = 2;    
        }//end if
  
       if(v1 &lt; v2){
          return false;
       }//end if
  
       return true;
    }//end precedence method
  
   //converts infix expression into postfix expression
   public static String convertToPostfix(String infixExp)
    {
       String postFix = "The Postfix Expression is: ";
       Stack&lt;Character&gt; stack = new Stack&lt;Character&gt;();
       char character = ' ';
  
       for(int i = 0; i &lt; infixExp.length(); i++)
       {
           character = infixExp.charAt(i);
  
           //determine if character is an operator
           if(character == '*' || character == '-' || character == '/' || character == '+')
           {
               while(!stack.empty() &amp;&amp; precedence(stack.peek(), character)){
                   postFix += stack.pop();
               }//end while
               stack.push(character);
           }
           else if(character == '(') //check for left parenthesis
           {
               stack.push(character);
           }
           else if (character == ')') 
           {
               while(!stack.peek().equals('(') &amp;&amp; !stack.isEmpty()){ //add characters until left parenthesis
                   postFix += stack.pop();
               }//end while
  
               if(!stack.isEmpty() &amp;&amp; stack.peek().equals('(')){
                   stack.pop(); // pop/remove left parenthesis
               }
           }
           else
           {
               postFix += character;
           }//end if
       }//end for
       while(!stack.empty()) //add the remaining elements of stack to postfix expression
       {
           if(stack.peek().equals('('))
           {
               postFix = "There is no matching right parenthesis.";
               return postFix;
           }
           postFix += stack.pop();
       }
           return postFix;
   }//end convertToPostfix
  }
  </code></pre>