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 Find the Edge Connectivity of a Graph
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
A Guide to Iterator in Java
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
ArrayList trong java
How to Manually Authenticate User with Spring Security
Hướng dẫn sử dụng String Format trong Java
Java Program to Perform integer Partition for a Specific Case
Autoboxing và Unboxing trong Java
Guide to the Java TransferQueue
Hướng dẫn Java Design Pattern – Strategy
Template Engines for Spring
Java Program to Implement Cartesian Tree
An Intro to Spring Cloud Task
Java Program to Solve Tower of Hanoi Problem using Stacks
Form Validation with AngularJS and Spring MVC
Convert Hex to ASCII in Java
Java Program to Check Cycle in a Graph using Topological Sort
Jackson JSON Views
Java Program to Check whether Directed Graph is Connected using BFS
An Intro to Spring Cloud Contract
Java Program to Perform Quick Sort on Large Number of Elements
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
How to Read a Large File Efficiently with Java
Transaction Propagation and Isolation in Spring @Transactional
Introduction to Java 8 Streams
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Java Program to Implement HashMap API
Jackson Exceptions – Problems and Solutions
What is Thread-Safety and How to Achieve it?