1

Hey guys ive come stuck once again, I can manage to push items onto a stack and pop them off. I even got working a stack with open and close brackets. But when I come to do it with () or {} or [] brackets with my code I created it seems to ignore the if statement and goes to else any ideas why.

Main.java

public class DifferentBrackets {

    public static void main(String[] args) {
        Stack brackets = new Stack();
        Scanner k = new Scanner(System.in);
        System.out.println("* to Terminate or Enter bracket : ");
        String tempBrack = k.next();
        while (!tempBrack.equals("*")) {
            System.out.println("* to Terminate or Enter bracket : ");

            switch (tempBrack) {
                case "(":
                    brackets.push("(");
                    System.out.println(tempBrack + " is added");
                    break;

                case "{":
                    brackets.push("{");
                    System.out.println(tempBrack + " is added");
                    break;

                case "[":
                    brackets.push("[");
                    System.out.println(tempBrack + " is added");
                    break;

                case ")":

                    if (tempBrack.equals(brackets.arrayTop()))
                            {
                            System.out.println(brackets.pop() + " is popped");
                            }
                    else if(tempBrack.equals(brackets.arrayTop()))
                    {
                        System.out.println("Closed Bracket Doesnt Match Top Open Bracket");
                    }
                    else 
                    {
                        System.out.println("Closed Bracket Doesnt Match Top Open Bracket");
                    }
                    break;

//            case "}":
//                    if (tempBrack.equals(brackets.arrayTop()))
//                            {
//                            System.out.println(brackets.pop() + " is popped");
//                            }
//                    else if (!tempBrack.equals(brackets.arrayTop()))
//                    {
//                        System.out.println("Closed Bracket Doesnt Match Top Open Bracket");
//                    }
//
//                    break;

//                case "]":
//                    if (tempBrack.equals(brackets.arrayTop()))
//                            {
//                       if (!tempBrack.equals(brackets.arrayTop()))
//                    {
//                        System.out.println("Closed Bracket Doesnt Match Top Open Bracket");
//                    }
//                    break;
            }

            System.out.println("* to Terminate or Enter bracket : ");
            tempBrack = k.next();
        }

    }
}

Heres my Stack.java

import java.util.ArrayList;

public class Stack
{
    private ArrayList<String> a;

    public Stack()
    {
       a = new ArrayList(10);       //initial capacity of 10
    }

    public boolean isEmpty()
    {
         return a.isEmpty();
    }

    public String pop()          //pop integer element
    {
        String last;
        last = a.remove((a.size()- 1));
        return(last);      //underflow will crash
    }

    public void push(String x)      //push integer element
    {
        a.add(x);
    }

    public String arrayTop()
    {
        return(a.get(a.size() -1));
    }
}
1
  • What is your problem? I don't get it. Btw your else if is useless as it checkes the same condition as your if Commented Dec 15, 2013 at 14:43

2 Answers 2

4

When you add a ")" you want to check if the top of the stack contains a "(", not a ")". But this is what you actually test here.

if (tempBrack.equals(brackets.arrayTop()))

is the same as (let's say your stack contains just "(" and you entered ")") :

if (")".equals("("))

which is obviously false.

You have to make the check in a way that when you're in a closing parenthesis/bracket, you have to check if the top of the stack contains an opening one.

case ")":

    if ("(".equals(brackets.arrayTop())){
        System.out.println(brackets.pop() + " is popped");
    }
    else {
        System.out.println("Closed Bracket Doesnt Match Top Open Bracket");
    }
    break;

The same logic applies for "{" and "[".

Note that :

  1. The API already provides a Stack class, no need to reinvent the wheel, you should use this one.
  2. Consider using a Stack of Characters, not Strings
  3. You may end your program if a closing bracket doesn't match a top open one.
Sign up to request clarification or add additional context in comments.

Comments

0

Please see the comments for an explanation. To the OP, please read and understand ZouZou's answer.

import java.util.Scanner;

public class DifferentBrackets {

    public static void main(String[] args) {
        Stack brackets = new Stack();
        Scanner k = new Scanner(System.in);
        System.out.println("* to Terminate or Enter bracket : ");
        String tempBrack = k.next();

        while (!tempBrack.equals("*")) {

            // if the user added an opening bracket
            // push it on the stack
            if (isOpeningBracket(tempBrack)) {
                brackets.push(tempBrack);
            }
            else if (isClosingBracket(tempBrack)) {
                // first check to see of the element at the 
                // top of the stack is the mirror of the closing bracket
                if (brackets.arrayTop().equals(mirror(tempBrack))) {
                    // if it is pop it from the stack
                    System.out.println(brackets.pop() + " is popped");
                }
                else {
                    System.out.println("Closed Bracket Doesnt Match Top Open Bracket");
                }
            }
            else {
                System.out.println(tempBrack + " is an unsuported character");
            }
            tempBrack = k.next();
        }
        // if there is nothing on the stack after the user enters "*"
        // the user has entered balanced brackets  
        if (brackets.isEmpty()) {
            System.out.println("Congrats your brackets are balanced");
        }
        else {
            System.out.println("Sorry your brackets are not balanced");
        }
        k.close();
    }

    // find the closing bracket's mirror
    public static String mirror(String str) {
        switch (str) {
        case ")":
            return "(";
        case "]":
            return "[";
        case "}":
            return "{";
        default:
            return null;
        }
    }

    public static boolean isOpeningBracket(String str) {
        return str.equals("(") || str.equals("{") || str.equals("[");
    }

    public static boolean isClosingBracket(String str) {
        return str.equals(")") || str.equals("}") || str.equals("]");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.