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:
Spring – Injecting Collections
Guide to Java 8 groupingBy Collector
Phân biệt JVM, JRE, JDK
Custom Thread Pools In Java 8 Parallel Streams
Java Program to Implement Attribute API
Bootstrapping Hibernate 5 with Spring
CyclicBarrier in Java
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Join and Split Arrays and Collections in Java
Hướng dẫn Java Design Pattern – Bridge
Java Program to Implement Shunting Yard Algorithm
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Setting the Java Version in Maven
Java Program to Create the Prufer Code for a Tree
Sử dụng CyclicBarrier trong Java
Send email with JavaMail
Simple Single Sign-On with Spring Security OAuth2
Java Byte Array to InputStream
A Guide to Java SynchronousQueue
Collect a Java Stream to an Immutable Collection
Java Program to Implement Gauss Seidel Method
Java Program to Implement Quick Sort with Given Complexity Constraint
Java Program to Represent Graph Using Incidence List
Java Program to subtract two large numbers using Linked Lists
Updating your Password
Spring 5 WebClient
Java Program to Implement Trie
Java 8 Collectors toMap
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
The Order of Tests in JUnit
Logout in an OAuth Secured Application
Using Java Assertions