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:
Supplier trong Java 8
Spring Boot - Flyway Database
So sánh HashMap và Hashtable trong Java
An Introduction to ThreadLocal in Java
Java Program to Perform LU Decomposition of any Matrix
Java – File to Reader
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Limiting Query Results with JPA and Spring Data JPA
Getting a File’s Mime Type in Java
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Mix plain text and HTML content in a mail
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Kết hợp Java Reflection và Java Annotations
Java Program to Implement Splay Tree
An Intro to Spring Cloud Task
Lấy ngày giờ hiện tại trong Java
Quick Intro to Spring Cloud Configuration
Introduction to Spring Data REST
Java Program to Implement DelayQueue API
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
What is a POJO Class?
Custom Cascading in Spring Data MongoDB
Spring Boot - Cloud Configuration Server
Spring Security Registration – Resend Verification Email
Spring WebClient Requests with Parameters
Spring Boot - Build Systems
Enum trong java
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Why String is Immutable in Java?
ArrayList trong java
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Java Program to Implement Control Table