Checking that the brackets in an expression match – Java

Over the next few days I will be posting programs that I have been practising for interviews.

This one is a simple brackets checking program where it will check to see if the brackets in the expression enter match. It works with other data also as it will simply ignore them. The program it’s self uses a stack to push the first bracket to the stack. When the matching bracket is found it would then pop the bracket from the stack. If the matching bracket is not found it would then return false.

Some sample data:

Enter: {2+5[45]}
Result: True because all brackets are matching in the expression.

Enter: (09+-94){76]
Result: False all brackets in the expression do not match.


public class Brackets
{
    public static boolean matchingBrackets(String word)
    {

        //setting up the stack
        Stack<Character> stack = new Stack<Character>();

        //declaring a character
        char character;

        // Will loop until the end of the string
        for(int i=0; i<word.length(); i++)
        {
            //setting the char to the different characters in the string
            //increments with the loop
            character = word.charAt(i);

            //seeing if the current character is a ( bracket
            if(character == '(')
            {
                //pushing the character into the stack
                stack.push(character);
            }//if

            //seeing if the current character is a { bracket
            else if(character == '{')
            {
                //pushing the character into the stack
                stack.push(character);
            }//else if

            //seeing if the current character is a [ bracket
            else if(character=='[')
            {
                //pushing the character into the stack
                stack.push(character);
            }//else if

            //Checking to see if the next character is ) bracket
            else if(character == ')')
            {
                //making sure there is something in the stack already
                //if not it returns false
                if(stack.empty())
                {
                    System.out.println("False");
                    return false;

                }//if

                //checking to see if there if the last thing added to the stack was an ( bracket
                else if(stack.peek() == '(')
                {
                    //because the program has found a match. It now can remove the ( bracket.
                    // So it can move onto the next if there is one.
                    stack.pop();
                }//else if

                //if the ( bracket is not found it will then return false.
                else
                {
                    System.out.println("False");
                    return false;
                }//else
            }//else if


            //Checking to see if the next character is } bracket
            else if(character == '}')
            {
                //making sure there is something in the stack already
                //if not it returns false
                if(stack.empty())
                {
                    System.out.println("False");
                    return false;
                }//if

                //checking to see if there if the last thing added to the stack was an { bracket
                else if(stack.peek() == '{')
                {
                    //because the program has found a match. It now can remove the ( bracket.
                    // So it can move onto the next if there is one.
                    stack.pop();
                }//else if

                //if the { bracket is not found it will then return false.
                else
                {
                    System.out.println("False");
                    return false;
                }//else
            }//else if


            //Checking to see if the next character is ] bracket
            else if(character == ']')
            {
                //making sure there is something in the stack already
                //if not it returns false
                if(stack.empty())
                {
                    System.out.println("False");
                    return false;
                }//if

                //checking to see if there if the last thing added to the stack was an [ bracket
                else if(stack.peek() == '[')
                {
                    //because the program has found a match. It now can remove the [ bracket.
                    // So it can move onto the next if there is one.
                    stack.pop();
                }//else if

                //if the [ bracket is not found it will then return false.
                else
                {
                    System.out.println("False");
                    return false;
                }//else
            }//else if

        }//for
        System.out.println("True");

        //returning true when the brackets are all correct
        return true;
    }//boolean

    public static void main(String[] args)
    {
        //Scanner for reading in users answer
        Scanner keyboard = new Scanner(System.in);

        //string to store users answer in
        String userInput="";

        //print satement so they know what to do
        System.out.print("Please enter the (){}[] you'd like to test. Feel free to add other numbers and letters in");

        //reading in there input
        keyboard.next();

        //calling the method and passing it the users input
        matchingBrackets(userInput);
    }//main
}//class

3 thoughts on “Checking that the brackets in an expression match – Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s