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:
A Guide to Java HashMap
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Java – Convert File to InputStream
Static Content in Spring WebFlux
The Spring @Controller and @RestController Annotations
Control the Session with Spring Security
Functional Interface trong Java 8
An Intro to Spring Cloud Security
Java Program to Implement ScapeGoat Tree
CharSequence vs. String in Java
Java Program to Implement Bresenham Line Algorithm
List Interface trong Java
Java Program to Perform integer Partition for a Specific Case
Java Program to Implement LinkedBlockingDeque API
Concatenating Strings In Java
Java Program for Douglas-Peucker Algorithm Implementation
How to Get a Name of a Method Being Executed?
Setting the Java Version in Maven
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Java – String to Reader
Java Program to Implement Dijkstra’s Algorithm using Queue
Apache Commons Collections Bag
Spring Boot - Bootstrapping
Java Program to Generate Random Numbers Using Probability Distribution Function
Limiting Query Results with JPA and Spring Data JPA
Custom Thread Pools In Java 8 Parallel Streams
Spring Boot - Code Structure
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
The Registration API becomes RESTful
HttpClient Basic Authentication
Guide to Dynamic Tests in Junit 5
Java Program to implement Dynamic Array