This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
here is my toString method that is in my linkedStackClass and you'll notice where it says size, well size will be entered in the gui portion of my code but how do I get my toString to see what size is equal to?
I am going to go out on a limb and assume this is related to your other thread from earlier today. This is one of those instances where recursion can be your friend. I whipped up the following example using your LinkedStackClass code in your other thread. (It's runnable but I had to slightly alter it to account for your other classes it references but which I do not have so it would compile. It no longer implements any interface and the StackUnderrunExceptions are now just plain Exception).
Notice I renamed your toString() to printStack(). Since you only get one shot at overriding toString(), is this really what you wanted to use it for?
As for how to get your toString() method to see the value of the size, well...the size variable would have to be an instance variable visible to the toString() method since you haven't declared any size variable local to the method.
Joined: May 25, 2009
Yup that is correct. Well I might as well put the rest of my code out there. This is my DropOutStack Class maybe it would be easier to override the toString in here? I guess I'm kinda confused about some of the code you added and how it works like this part. return stackTop == null ? "" : stackTop.printStack();
Joined: Oct 26, 2010
Dustin Schreader wrote:I guess I'm kinda confused about some of the code you added and how it works like this part. return stackTop == null ? "" : stackTop.printStack();
In more verbose code that would read:
If you examine your StackNode class you will see I added the method printStack() to it.