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.

/*
 *  Java Program to Check for balanced paranthesis by using Stacks
 */
 
import java.util.*;
 
public class ParenthesisMatching
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        /* Creating Stack */
        Stack<Integer> stk = new Stack<Integer>();
        System.out.println("Parenthesis Matching Test\n");
        /* Accepting expression */
        System.out.println("Enter expression");
        String exp = scan.next();        
        int len = exp.length();
        System.out.println("\nMatches and Mismatches:\n");
        for (int i = 0; i < len; i++)
        {    
            char ch = exp.charAt(i);
            if (ch == '(')
                stk.push(i);
            else if (ch == ')')
            {
                try
                {
                    int p = stk.pop() + 1;
                    System.out.println("')' at index "+(i+1)+" matched with ')' at index "+p);
                }
                catch(Exception e)
                {
                    System.out.println("')' at index "+(i+1)+" is unmatched");
                }
            }            
        }
        while (!stk.isEmpty() )
            System.out.println("'(' at index "+(stk.pop() +1)+" is unmatched");
    }
}
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

Related posts:

Java Program to Implement Borwein Algorithm
Netflix Archaius with Various Database Configurations
Java Program to Implement ScapeGoat Tree
Introduction to Spring Security Expressions
Java Program to Find a Good Feedback Vertex Set
Sorting in Java
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Java Program to Implement Binary Tree
Introduction to Spring Cloud OpenFeign
Immutable Objects in Java
Java Program to Find Transitive Closure of a Graph
Java Program to Implement Hash Tables chaining with Singly Linked Lists
A Guide to Spring Boot Admin
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
HttpClient Basic Authentication
Java Program to Compute the Area of a Triangle Using Determinants
Auditing with JPA, Hibernate, and Spring Data JPA
Java Program to Compute Cross Product of Two Vectors
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Tạo chương trình Java đầu tiên sử dụng Eclipse
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Java Program to Implement Gauss Jordan Elimination
Guide to BufferedReader
Hashing a Password in Java
Spring WebFlux Filters
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Java Program to Implement AVL Tree
Java Program to Implement Sparse Matrix
How to Break from Java Stream forEach
Introduction to PCollections
Java Program to Perform Polygon Containment Test
Java Program to Check Whether a Directed Graph Contains a Eulerian Path