Java Program to Check for Balanced Parenthesis by using Stacks

This is a Java Program to Check for balanced parenthesis by using Stacks. Parenthesis matching is commonly used for evaluating arithmetic expressions and in editors for validating syntax.

Here is the source code of the Java Program to check for balanced parenthesis by using stacks. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /*
  2.  *  Java Program to Check for balanced paranthesis by using Stacks
  3.  */
  4.  
  5. import java.util.*;
  6.  
  7. public class ParenthesisMatching
  8. {
  9.     public static void main(String[] args) 
  10.     {
  11.         Scanner scan = new Scanner(System.in);
  12.         /* Creating Stack */
  13.         Stack<Integer> stk = new Stack<Integer>();
  14.         System.out.println("Parenthesis Matching Test\n");
  15.         /* Accepting expression */
  16.         System.out.println("Enter expression");
  17.         String exp = scan.next();        
  18.         int len = exp.length();
  19.         System.out.println("\nMatches and Mismatches:\n");
  20.         for (int i = 0; i < len; i++)
  21.         {    
  22.             char ch = exp.charAt(i);
  23.             if (ch == '(')
  24.                 stk.push(i);
  25.             else if (ch == ')')
  26.             {
  27.                 try
  28.                 {
  29.                     int p = stk.pop() + 1;
  30.                     System.out.println("')' at index "+(i+1)+" matched with ')' at index "+p);
  31.                 }
  32.                 catch(Exception e)
  33.                 {
  34.                     System.out.println("')' at index "+(i+1)+" is unmatched");
  35.                 }
  36.             }            
  37.         }
  38.         while (!stk.isEmpty() )
  39.             System.out.println("'(' at index "+(stk.pop() +1)+" is unmatched");
  40.     }
  41. }

Parenthesis Matching Test
 
Enter expression
(a+(b*c)-d)
 
Matches and Mismatches:
 
')' at index 8 matched with ')' at index 4
')' at index 11 matched with ')' at index 1
 
 
 
Parenthesis Matching Test
 
Enter expression
((x+y/(z-w))
 
Matches and Mismatches:
 
')' at index 11 matched with ')' at index 7
')' at index 12 matched with ')' at index 2
'(' at index 1 is unmatched
 
 
 
Parenthesis Matching Test
 
Enter expression
(a+b*(c-d)-(e-f/(g+h*(k-i)/(l-j+k
 
Matches and Mismatches:
 
')' at index 10 matched with ')' at index 6
')' at index 26 matched with ')' at index 22
'(' at index 28 is unmatched
'(' at index 17 is unmatched
'(' at index 12 is unmatched
'(' at index 1 is unmatched

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.