boolean Match(char c) {
    if (this.type == '[' && c == ']')
        return true;
    if (this.type == '{' && c == '}')
        return true;
    if (this.type == '(' && c == ')')
        return true;
    return false;
}
    Stack<Bracket> opening_brackets_stack = new Stack<Bracket>();
    for (int position = 0; position < text.length(); ++position) 
    {
        char next = text.charAt(position);
        if (next == '(' || next == '[' || next == '{') 
        {
            // Process opening bracket, write your code here
            Bracket temp = new Bracket(next,position);
            opening_brackets_stack.push(temp);
        }
        if (next == ')' || next == ']' || next == '}') 
        {
            // Process closing bracket, write your code here
            try{
                Bracket item = opening_brackets_stack.pop();
                if(!item.Match(next)) 
                {  //not match
                    System.out.println(position+1);  
                    return;
                }
            }   
            catch(EmptyStackException e){}
        }
    }
    // Printing answer, write your code here
    try{
        if(opening_brackets_stack.isEmpty()) 
        {
          System.out.println("Success");
        }
        else {
            Bracket item = opening_brackets_stack.pop();
            //print position of first unmatched opening bracket
            System.out.println(item.position+1);
        }
    }
    catch (EmptyStackException e){}
}
i am getting wrong answer in cases like "}","()}" in which bracket is at last position. i am supposed to get answer "1","3" respectively for above cases but i am getting "Success". In all other cases, it works perfectly fine. What should I do?
 
    