code: private int getClosingParenthesisPosition(String text, int opening)
     {
        if (text.charAt(opening) != '(')
        {
           return -1;
        }
  
        int count = 0;
        for (int i = opening; i < text.length(); i++)
        {
           char c = text.charAt(i);
           switch (c)
           {
              case '(':
              {
                 ++count;
                 break;
              }
  
              case ')':
              {
                 --count;
                 if (count == 0)
                 {
                    return i;
                 }
                 break;
              }
           }
        }
  
        return -1;
     }
docs: Look for the closing parenthesis corresponding to the one at position
  represented by the opening index.
  
  @param text input expression
  @param opening opening parenthesis index
  @return closing parenthesis index