# How to determine an index of first redundant parenthesis in arethmetic expression? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to determine an index of first redundant parenthesis in arethmetic expression? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

for example: if an input is (5*x) the return result is: “Parentheses are placed correctly”

if an input is (((5*x) the return result is: “Parentheses are placed incorrectly” “Quantity of redundant opening parenthesis: 3”

if an input is (5*x)+10*y)+5*z) my return result must be: “Parentheses are placed incorrectly” “Index of first redundant closing parenthesis: 10” – This is what I can not understand how to do……………..

How to implement an output, that shows an index of first redundant closing parenthesis?

This is my code:

```    public static void main(String[] args) {

System.out.println("Enter an arithmetic expression:");

Scanner in = new Scanner(System.in);
String arithmeticExpression = in.nextLine();

System.out.println(parenthesesCheckup(arithmeticExpression));

}

public static String parenthesesCheckup(String arithmeticExpression) {

char openingParenthesis = '(';
char closingParenthesis = ')';

Stack<Character> stack = new Stack<Character>();

char array[] = arithmeticExpression.toCharArray();

for (char current : array) {
if ((current == closingParenthesis) && stack.empty()) {
stack.push(current);
char previousStack = stack.peek();
switch (current) {
case ')':
if (previousStack == '(')
stack.pop();
return "Parentheses are placed incorrectlyn" +
"Index of first redundant closing parenthesis: ";
}
}

if (current == openingParenthesis) {
stack.push(current);
}

if ((current == closingParenthesis) && !stack.empty()) {
char previousStack = stack.peek();
switch (current) {
case ')':
if (previousStack == '(')
stack.pop();
break;
}
}
}
if (stack.empty()) {
return "Parentheses are placed correctly";
} else {
return "Parentheses are placed incorrectlyn" +
"Quantity of redundant opening parenthesis: " + stack.size();
}
}
}
```

## Answer

How to implement an output, that shows an index of first redundant closing parenthesis?

Add a counter for the character indexes that you have processed. For example you could convert the for-each loop to a counting loop:

```for (int index = 0; index < array.length; index++) {
char current = array[index];
// ...
```

And then when you found an out of place closing parenthesis, append the current index to your string:

```    return "Parentheses are placed incorrectlyn" +
"Index of first redundant closing parenthesis: " + index;
```
We are here to answer your question about How to determine an index of first redundant parenthesis in arethmetic expression? - If you find the proper solution, please don't forgot to share this with your team members.