-3
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

import static java.lang.System.in;
import static java.lang.System.out;

/*
 *
 *
Use a stack to check parentheses, balanced and nesting
 *  The parentheses are: (), [] and {}
 *
 *  See:
 *  - UseAStack
 *
 */

public class Ex3CheckParen {


    public static void main(String[] args) {
        new Ex3CheckParen().program();
    }


    void program() {
        // All should be true
        out.println(checkParentheses("()"));

        out.println(checkParentheses("(()())"));

        out.println(!checkParentheses("(()))")); // Unbalanced

        out.println(!checkParentheses("((())")); // Unbalanced


        out.println(checkParentheses("({})"));

        out.println(!checkParentheses("({)}"));  // Bad nesting

        out.println(checkParentheses("({} [()] ({}))"));

        out.println(!checkParentheses("({} [() ({)})"));  // Unbalanced and bad nesting

    }


    // This is interesting because have to return, but what if no match?!?

    boolean checkParentheses(String str) {
        Deque<Character> stack = new ArrayDeque<>();

        String k = "({[";
        String s = ")]}";
        for (int i = 0; i < str.length(); i++) {
            if (k.contains(String.valueOf(str.charAt(i)))) {
                stack.push(str.charAt(i));
            } else if (s.contains(String.valueOf(str.charAt(i)))) {

                if (matching(stack.peek()) == str.charAt(i)) { //ILLEGAL ARGUMENT EXCEPTION HERE
                    return true;
                }
            } else {
                return false;
            }
        }
        return false;
    }


    char matching(char ch) {
        //char c =  must initialize but to what?!

        switch (ch) {
            case ')':
                return '('; // c = '('

            case ']':
                return '[';

            case '}':
                return '{';

            default:
                // return c;

                throw new IllegalArgumentException("No match found");
        }
    }
}

I'm getting an exception error in the if statement containing matching. Unable to figure out the cause.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
Netizen110
  • 1,644
  • 4
  • 18
  • 21
  • 3
    dont post us a wall of code and let us figure it out. in what line are you getting the exception? you do realizie that you throw an IllegalArgumentException in the default case?! give us the stacktrace – Philipp Sander Jan 08 '18 at 16:01
  • 1
    @PhilippSander: They do indicate where they're getting the exception, but the point does stand; they are indeed throwing it themselves. – Makoto Jan 08 '18 at 16:05
  • 3
    "_Unable to figure out the cause_" It's right where you wrote it `throw new IllegalArgumentException("No match found");`. – takendarkk Jan 08 '18 at 16:06

1 Answers1

1

Maybe something like this?

public class Ex3CheckParen {

public static void main(String[] args) {
    new Ex3CheckParen().program();
}

void program() {
    // All should be true
    out.println(checkParentheses("()"));

    out.println(checkParentheses("(()())"));

    out.println(!checkParentheses("(()))")); // Unbalanced

    out.println(!checkParentheses("((())")); // Unbalanced

    out.println(checkParentheses("({})"));

    out.println(!checkParentheses("({)}")); // Bad nesting

    out.println(checkParentheses("({} [()] ({}))"));

    out.println(!checkParentheses("({} [() ({)})")); // Unbalanced and bad nesting

}

// This is interesting because have to return, but what if no match?!?

boolean checkParentheses(String str) {
    Deque<Character> stack = new ArrayDeque<>();

    String k = "({[";
    String s = ")]}";
    for (int i = 0; i < str.length(); i++) {
        if (k.contains(String.valueOf(str.charAt(i)))) {
            stack.push(str.charAt(i));
        } else if (s.contains(String.valueOf(str.charAt(i)))) {

            if (matching(stack.peek(), str.charAt(i))) {
                return true;
            }
        } else {
            return false;
        }
    }
    return false;
}

boolean matching(char ch1, char ch2) {

    if ('(' == ch1 && ch2 == ')' || '[' == ch1 && ch2 == ']' || '{' == ch1 && ch2 == '}') {
        return true;
    }

    return false;

}

}

In my opinion by the way, the method checkParentheses(String str) should look more like this:

boolean checkParentheses(String str) {
    Deque<Character> stack = new ArrayDeque<>();

    String open = "({[";
    String close = ")]}";
    int length = 0;
    for (int i = 0; i < str.length(); i++) {
        char currentChar = str.charAt(i);
        if (open.contains(String.valueOf(currentChar))) {
            stack.push(currentChar);
            length++;
        } else if (close.contains(String.valueOf(currentChar))) {
            if (!stack.isEmpty() && matching(stack.peek(), currentChar)) {
                stack.pop();
                length--;
            } 
            else {
                return false;
            }

        } else {
            return false;
        }
    }

    if (length == 0)
        return true;

    return false;
}

But it is totally up to you...