简体   繁体   中英

Parenthesis checker in java

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 [()]{}{[()()]()} and false for [(]) . I have made a stack class of my own for this problem:

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;
}

}

The checkValid method below takes a String input and returns true if the parentheses matches, and false if they do not match.

    public static Boolean checkValid(String str){
        char sym,prev;
        Stack s = new Stack(str.length());
        for(int i=0; i<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 == '(' && character2 == ')')
            return true;
        else if(character1 == '{' && character2 == '}')
            return true;
        else if(character1 == '[' && character2 == ']')
            return true;
        else
            return false;
    }
}

Is there a way to print the position of the unmatched parentheses?

If your Stack, instead of holding chars , would hold a class that contains both the char and the index of that char in the input String, you'll be able to print the index of the unmatched parentheses.

EDIT:

This solution is only required if you want the indices of both unmatched parentheses that failed the isPairMatch test.

For example, if you have the String "[{}{}{})", the pair that doesn't match are the first "[" and last ")", whose indices are 0 and 7.

If you only need the index of the first unmatched parenthesis (ie the first "[" whose index is 0), you simply check the size of the Stack after the removal of that parenthesis. In this example, the stack would be empty when the last pair is checked, so the size of the stack would be 0, which is the index of the first char of the failed isPairMatch test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM