This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Can it be possible the java.util.Stack empty() function is not returning the correct value.
The probelm is sometime I'm getting java.util.EmptyStackException from poppinout funtion even if I check stack.empty() before calling stack.pop function.
I'm using org.xml.sax.ContentHandler to fetch the first 500 character until the first period(&quot;.&quot;) in the XML. The code look something like this.
No, you'll find your error is the obvious one: you are popping from the stack more than you are pushing onto it.
By the way, if you want people to read your code, you should post it inside the "code" tags. I have to say I didn't read the code you posted; unindented code is too hard to read. You can edit your post by clicking on the "Edit" button.
Joined: Mar 19, 2009
Thanks for your reply.
Sorry for posting the unformatted code. I was not aware of the standard practise of putting the code in this editor.
As you said I'm popping from the stack more than pushing onto it, does that mean the if condition I put "stack.empty()" before calling the Pop function is not working? In all the scenario where I'm popping out I have put the if condition to check whether stack is empty or not.
The simple answer is that Stack.empty() won't be returning incorrect values. You haven't included a standalone test case to reproduce your problem so we can't offer much more help. The only reason I can think for for pop() to fail after empty() returns false is if the stack is shared by multiple threads.