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:
Từ khóa throw và throws trong Java
Spring Security and OpenID Connect
Java Program to subtract two large numbers using Linked Lists
Filtering and Transforming Collections in Guava
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Check if there is mail waiting
Disable DNS caching
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Java 8 Streams peek() API
Converting String to Stream of chars
Java Program to Implement Shell Sort
ETags for REST with Spring
Converting Between an Array and a Set in Java
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Tiêu chuẩn coding trong Java (Coding Standards)
Java Program to Generate Randomized Sequence of Given Range of Numbers
Comparing Long Values in Java
Convert char to String in Java
Các kiểu dữ liệu trong java
Java 9 Stream API Improvements
Tìm hiểu về xác thực và phân quyền trong ứng dụng
Spring Boot - Application Properties
Java Program to Evaluate an Expression using Stacks
Java – Write to File
Java Program to Implement Segment Tree
Java Program to Implement Fibonacci Heap
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Performance Difference Between save() and saveAll() in Spring Data
Java Program to Implement Hash Tree
Giới thiệu Json Web Token (JWT)
Tìm hiểu về Web Service
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5