File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Exception in matching parentheses using stacks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of RabbitMQ in Depth this week in the Open Source forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception in matching parentheses using stacks" Watch "Exception in matching parentheses using stacks" New topic

Exception in matching parentheses using stacks

clarkson Joy

Joined: May 10, 2014
Posts: 4
I am trying to write a program that checks for parentheses matching using stacks.This is my ArrayStack class.

And this is my matching class.

This is EmptyStackException class
But the problem is when I compile matching I get an error as unreported exception EmptyStackException.must be caught or declared to be thrown.
I think the problem is with exceptions which I don't have a good knowledge of.I am stuck here for days and because of this exception issue I am unable to study the rest of data structures. So any help on how I can fix this and run this would be greatly helpful.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14801

Welcome to the Ranch!

The problem is that in class Matching, in the method matching(), you are calling several methods from class ArrayStack, for example push() and topAndpop() - these methods may throw OverFlowException and EmptyStackException, and you are not handling those exceptions in the matching() method. You must either handle these exceptions in the matching() method by adding a try-catch block to the method, or you must declare that the matching() method can throw these exceptions by adding a "throws" clause to the method declaration.

See The Catch or Specify Requirement for a more detailed explanation.

There are, however, more things wrong with your code:

You are comparing strings using the == operator, for example in the matching() method:

That will not work. You must use equals() instead to compare strings:

The == operator compares object references, not the content of objects. == will only evaluate to true if the arguments on the left and right side refer to the exact same object. If you have two distinct String objects, the result will be false, even if the two String objects contain the same value.

These lines:

can be written far more concisely:

Note that the expression match_Stack.isEmpty() already evaluates to a boolean value (true or false). No need to say "if this expression is true, then return true, else return false".

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
clarkson Joy

Joined: May 10, 2014
Posts: 4
Thank you very much for the answer.I was stuck here for a long timeI thought because exceptions occur at push() and topAndpop() methods and there I have thrown them I don't have to do anything in Matching class.It got rid of those problems but mine always evaluate to true,even if I input something like "{","]" which is wrong matching
Mike. J. Thompson
Ranch Hand

Joined: Apr 17, 2014
Posts: 398
In your matching method you have a for-loop that processes your array of parentheses. You push each opening parenthesis, and you pop an item each time you see a closing parenthesis.

Then after your loop you have the following code:

So, if your array of parentheses contains an opening and a closing brace (of any type), how many items will be on the stack after you finish processing your array, and which branch of the if-statement will run?

Once you have figured that out, I think you'll find that your logic in the for-loop isn't quite right. It won't match closing parentheses to opening parentheses.

And one final piece of advice. I suggest you get into the habit of always adding opening and closing curly braces to if-statements and loops etc, even if the body only contains a single statement. That will prevent some subtle bugs if you aren't careful when editing your code.
clarkson Joy

Joined: May 10, 2014
Posts: 4
I was able to correct the always true issue,I introduced a boolean variable isMatch and made t he change in the code as

But why do I have to check after the for loop if the stack is empty.I s it because I have other Objects in the input array other than parantheses
Mike. J. Thompson
Ranch Hand

Joined: Apr 17, 2014
Posts: 398
Well you put that check in, and since you haven't given us your exact requirements we can't tell you if you've met them or not.

Guessing, I'd say its to cover situations like '{', or '()(' where you have enclosed parentheses.
Winston Gutkowski

Joined: Mar 17, 2011
Posts: 8657

clarkson Jp wrote:I am trying to write a program that checks for parentheses matching using stacks.This is my ArrayStack class.

Could I suggest that you are confusing the issue (and probably squaring the chance of problems) by taking on both things at once.

The business of matching parentheses - with or without stacks - and making a Stack class are two totally separate issues; and I for one wouldn't try to tackle them together.

My suggestion: If you want to create a "parentheses matcher", use java.util.Stack. It's been around for a very long time, and has probably had more thought go into it than you are ever likely to be able to devote - and that's not a criticism, just a fact.

Alternatively, if you absolutely must write this all yourself from scratch, write your Stack (or ArrayStack) class first, and don't even think about moving on to the "matcher" part until you know that it works. Every time; and with any input.



Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
It is sorta covered in the JavaRanch Style Guide.
subject: Exception in matching parentheses using stacks