Question: Write a java program that reads lines of code from a text file called Parens.txt, and determines if the parentheses are balanced in each line.

Write a java program that reads lines of code from a text file called "Parens.txt", and determines if the parentheses are balanced in each line.

Create a stack to hold characters.

When you find an open parenthesis, push() the parentheses on the Stack.

When you find a close parenthesis, pop() the stack to retrieve the open paren to match the closed paren you just found.

If you ever try to pop() the stack and it is empty, The code will throw an exception. That exception indicates an unbalanced set of parentheses.

After reading the last character of a given line from the Parens.txt file, if there is anything left in the stack, the parentheses are unbalanced.

For example, the text file contains (single spaced) the following three lines:

print("Hello, there")

int x = (a/(b+c)-d

for (int x=0; x

The program would produce the following output:

Line 1: balanced

Line 2: unbalanced

Line 3: balanced

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!