title: Parenthesis checker in java
body: <p>I have made a parenthesis checker program in java that that reads in a text stream from standard input and uses a stack to determine whether or not its parentheses are properly balanced. For example, it should print true for <code>[()]{}{[()()]()}</code> and false for <code>[(])</code>. I have made a stack class of my own for this problem:</p>
  
  <pre><code>public class Stack {
  private char items[];
  private int top;
  
  Stack(int n){
      items = new char[n];
      top = -1; 
  }
  
  void push(char c){
      if(top == items.length-1){
          System.out.println("Stack full.");
          return;
      }
      top++;
      items[top] = c;
  }
  
  char pop(){
      if(isEmpty()){
          System.out.println("Stack empty");
          return (char)0;
      }
      char p;
      p = items[top];
      top--;
      return p;
  }
  
  boolean isEmpty(){
      if(top == -1)
          return true;
      else    
          return false;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The <code>checkValid</code> method below takes a String input and returns true if the parentheses matches, and false if they do not match.</p>
  
  <pre><code>    public static Boolean checkValid(String str){
          char sym,prev;
          Stack s = new Stack(str.length());
          for(int i=0; i&lt;str.length();i++){
              sym = str.charAt(i);
              if(sym == '(' || sym=='{' || sym=='['){
                  s.push(sym);
              }
              if(sym == ')' || sym=='}' || sym==']'){
                 if(s.isEmpty()){
                     return false;
                  }
                 else{
                      prev = s.pop();
                      if(!isPairMatch(prev,sym))
                          return false;
                 }
              }
  
          }
          if(!s.isEmpty())
              return false;
          return true;
      }
      public static boolean isPairMatch(char character1, char character2){
          if(character1 == '(' &amp;&amp; character2 == ')')
              return true;
          else if(character1 == '{' &amp;&amp; character2 == '}')
              return true;
          else if(character1 == '[' &amp;&amp; character2 == ']')
              return true;
          else
              return false;
      }
  }
  </code></pre>
  
  <p>Is there a way to print the position of the unmatched parentheses? </p>