wood burning stoves
The moose likes Beginning Java and the fly likes Stacks using Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stacks using Arrays" Watch "Stacks using Arrays" New topic

Stacks using Arrays

Shananne DuFrame
Ranch Hand

Joined: Nov 13, 2006
Posts: 51
Hello, this is a homework assignment I've been working on. From the existing StackClass class I removed the use of DataElement and IntElement (as requested by our teacher). But in changing my methods to do so I messed up somewhere along the way. It compiles, tells me whether the stack is empty or not, but seems when I put values in and try to print the stack with toString I am losing that stack, it is then appearing empty. I'm not quite sure what I did wrong. But it is really delaying me in my assignment in that we were really suppose to be concentrating on our reversePrint method that we are suppose to add. I even took this to the school tutor, with no luck she could not find the problem either. She did not know why my stack was being lost along the way. But I believe I narrowed it down to happening in the toString Method. Someone please check my code....

Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Hi, I don't know if I will be helpful or not, but I did want to point out a few things... the first thing, take a close look at your StackClass constructor, the one that takes an integer. maxSize is the name used for the value you pass in AND for the private variable that holds the max size for the class. so inside that method, whenever you say "maxSize", you are only referring to the value you passed in, and therefore you are never updating the private variable, so it will remain at 0. I think that is the main source of your problem, however that wasn't the only thing I noticed. You are pushing 8 things onto the stack, so make sure you have the space on the stack to push them all (ie. do not make your stack size 5 and then try to push 8 elements onto it). And two more things:
(1) in your push() method, you are comparing the length of the array to maxSize, but they are always going to be equal. test StackArray.length against maxSize inside any class instance, you will see that they are equal. "top" might come in handy here...
(2) take a closer look at this line:

maybe you're not starting at the very beginning of the stack...

Studying for SCJP 6
Shananne DuFrame
Ranch Hand

Joined: Nov 13, 2006
Posts: 51
Thanks. I did get it working. I changed quite a few things. I'm now going to start a new thread because my next question is on the reverse method.
Thanks for your help!!
I agree. Here's the link: http://aspose.com/file-tools
subject: Stacks using Arrays
It's not a secret anymore!