-3

I have a string like:

If ({{SQL}}.Employee.Title starts with 'Production') 
and  (substring of {{SQL}}.Employee.Title from '27' for '2' is not '30') 
and ({{SQL}}.Employee.HireDate is greater than or equal to '2000-01-01 00:00:00.000')  
then Pull  {{SQL}}.Title,  {{SQL}}.HireDate from Employee

From this expression, I want to find out whether round brackets are properly balanced or not in Java language.

One way is to create a counter variable which will be incremented as soon as I find out the opening bracket and decrement it when closing bracket is encountered. Based on the result, I can decide the result.

But this is not going to help for string like () i.e. not having any alpha numeric character between brackets.

Is there any way in which I can determine whether round brackets are balanced and there should be alpha numeric characters between these brackets. In case brackets are empty i.e. no character between opening and closing brackets, it should throw an error.

Ali
  • 3,373
  • 5
  • 42
  • 54
Pranav Shukla
  • 11
  • 2
  • 8
  • 3
    Don't use regex, it will get messy. use a `Stack` – TheLostMind Jul 11 '17 at 08:05
  • Using stack, how can I identify alpha numeric characters are there within brackets. – Pranav Shukla Jul 11 '17 at 08:14
  • 1
    think about it. If you get a opening bracket, then push it onto the stack and set a flag to true. So, the next element that is read, should not be a closing brace (error). Also, all the elements that are read after the opening brace should be alphanumeric (check https://stackoverflow.com/questions/12831719/fastest-way-to-check-a-string-is-alphanumeric-in-java) – TheLostMind Jul 11 '17 at 08:20
  • Regular expressions should be used for validating whether the word is valid in a regular language or not. Regular languages don't need stack to validate. Language with braces parity needs a stack, therefor regex is a very bad tool for this purpose. – xenteros Jul 11 '17 at 09:46

1 Answers1

1

You'll need a code similar to below one. It does use a Stack to track the number of opened/closed parantheses + remembers what was the last char occurence in order to track empty parantheses:

    String test = "{TEST}(A){";

    Stack<Integer> stack = new Stack<>();
    boolean lastCharIsParantheses = false;
    for (char c : test.toCharArray()) {
        switch (c) {
            case '{':
            case '(': {
                stack.push(1);
                lastCharIsParantheses = true;
                continue;
            }
            case '}':
            case ')':
                stack.pop();
                if (lastCharIsParantheses) {
                    throw new RuntimeException("Empty parantheses");
                }
        }
        lastCharIsParantheses = false;
    }
    if (!stack.empty()) {
        throw new RuntimeException("Not matching number of opened/closed parantheses");
    }
P.An
  • 355
  • 1
  • 9
  • 1
    Replace the Exception with RunTimeException and improve the switch. Also could you please explain what this code does? Refer to [answer]. Still I like this answer – xenteros Jul 11 '17 at 09:47