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:
Converting Iterator to List
Spring Boot - Apache Kafka
Java Program to Perform Insertion in a BST
Convert Hex to ASCII in Java
How to Delay Code Execution in Java
Spring AMQP in Reactive Applications
Spring Boot - Cloud Configuration Server
Java Program to Implement Doubly Linked List
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Java Program to Implement Treap
Spring Boot - Building RESTful Web Services
Java Program to Implement Pairing Heap
A Custom Media Type for a Spring REST API
Overview of Spring Boot Dev Tools
Java Program to Implement Min Hash
Guide to java.util.concurrent.Locks
LinkedHashSet trong Java hoạt động như thế nào?
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Java Program to Implement VList
Mapping Nested Values with Jackson
Debugging Reactive Streams in Java
Java Program to Implement Range Tree
Validate email address exists or not by Java Code
Spring MVC Setup with Kotlin
Hướng dẫn Java Design Pattern – Intercepting Filter
Spring Data MongoDB Transactions
Spring Cloud AWS – Messaging Support
Converting a List to String in Java
New Stream Collectors in Java 9
Java Program to Implement Queue
Java – String to Reader
Java Program to Generate a Sequence of N Characters for a Given Specific Case