Write a java program that takes as input an arithmetic expre
Write a java program that takes as input an arithmetic expression. The program outputs whether the expression contains matching grouping symbols. For example, the arithmetic expressions {25 + (3 – 6) * 8} and 7 + 8 * 2 contains matching grouping symbols. However, the expression 5 + {(13 + 7) / 8 - 2 * 9 does not contain matching grouping symbols.
Solution
import java.util.Scanner;
 import java.util.Stack;
public class ArithmeticExpression {
public static void main(String args[]) {
   Scanner scanner = new Scanner(System.in);
    System.out.println(\"Enter the arithmetic expression\");
    String expression = scanner.nextLine();
    //User input is read into expression
    boolean result=balancedParanthesis(expression);
    if(result)
        System.out.println(\"The expression is VALID\");
    else
        System.out.println(\"The expression is INVALID\");
}
public static boolean balancedParanthesis(String s) {
 Stack<Character> stack = new Stack<Character>();
 /*This stack stores opening grouping symbols like \'{\' and \'(\' */
 for(int i = 0; i < s.length(); i++) {
 /*Each character of the input expression is checked */
 char c = s.charAt(i);
 if(c == \'(\' || c == \'{\' ) {
 stack.push(c);
 }else if(c == \')\') {
 if(stack.isEmpty()) return false;
 if(stack.pop() != \'(\') return false;
}else if(c == \'}\') {
 if(stack.isEmpty()) return false;
 if(stack.pop() != \'{\') return false;
 }
 }
 return stack.isEmpty();
 }
 }

