title: Stack, Parentheses Matching
body: <p>So I'm working on some homework with PostFix and Infix Expressions. I'm running into a bit of a problem and can't seem to find where I'm having the issue. I can get the Infix to Postfix working...for the most part. Some of the equations I'm getting a ( or ) printed when I don't want it to be printed. Also when I have no matching parentheses I don't get an error like I want it to.</p>
  
  <pre><code>public String Infix(String equation) throws Exception{
      Stack stack=new Stack();
      boolean parensMatch=false;
      int priority=0;
      String temp="";
      for(int i=0; i&lt;equation.length(); i++){
          char c=equation.charAt(i);
          //if the character is equal to left paren, push
          if(c=='('){
              stack.push(c);
          }
          //if the character is equal to right paren, we start popping until we find a match
          else if(c==')'){
              try{
                  while(stack.peek()!='('){
                      temp+=stack.pop();
                  }
  
                  if(stack.peek()=='('){
                      char ch=stack.pop();
                      parensMatch=true;
                  }
  
                  if(parensMatch==false){
                      throw new Exception("Parens Not Match Error");
                  }
              }catch(Exception e){
                  System.out.println(e);
              }
              parensMatch=false;
          }
          //if the character is equal to an operator, we do some extra work
          //to figure out what is going to happen
          else if(c=='+' || c=='-' || c=='*' || c=='/' || c=='^'){
              char top=stack.peek();
              if(top=='^')
                  priority=2;
              else if(top=='*' || top=='/')
                  priority=1;
              else
                  priority=0;
              if(priority==2){
                  if(c=='*' || c=='/'){
                      temp+=stack.pop();
                  }
                  else if(c=='+' || c=='-'){
                      temp+=stack.pop();
                  }
                  else{
                      temp+=stack.pop();
                  }
              }
              else{
                  if(c=='*' || c=='/'){
                      temp+=stack.pop();
                      stack.push(c);
                  }
                  else if(c=='+' || c=='-'){
                      stack.push(c);
                  }
                  else{
                      stack.push(c);
                  }
              }
          }
          //if the character is a space, we ignore it and move on
          else if(c==' '){
              ;
          }
          //if the character is a letter, we add it to the string
          else{
              temp+=c;
          }
      }
      int len = stack.size();
      for (int j = 0; j &lt; len; j++)
         temp+=stack.pop();
      return temp;
  }
  </code></pre>
  
  <p>This is my Infix to Postfix method</p>
  
  <p><code>(((A + B) - (C - D)) / (E - F))</code> This is one of the expressions that I need to solve, and <code>AB+CD--(EF-/</code> is what I get when it prints to the screen. <code>((A</code> is another, this one should give me an error but <code>A((</code> is printed to the screen.</p>
  
  <p>I have been running the debug for quite a while and can't seem to get anywhere.</p>
  
  <p>Any help would be very helpful. I know it has something to with the code posted but I can't find the logic error. Thanks in advance!</p>
  
  <p>So I added a new function to help with matching parens that I think will be useful. It takes the equation and just counts to see if they match or not.</p>
  
  <pre><code>public static int matchingParens(String equation){
      int match=0;
  
      for(int i=0; i&lt;equation.length(); i++){
          char c=equation.charAt(i);
          if(c=='(')
              match++;
          else if(c==')')
              match--;
          else
              ;
      }
  
      return match;
  }
  </code></pre>