Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Having problem with stacks  RSS feed

 
Joan Tan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming a user input a number of website url ranging from 1-10 into a stack and next has to input the number of times he/she clicks the back button in order to pop the website url out from the stack. How do I display the stack as empty once the website url has all been popped out from the stack if the user clicks the back button more than the number of website url he/she entered?
Right now, my coding just display all the "pop" urls as empty instead of showing the rest of the "pop" urls. This is what I needed for the outcome.
Number of times back button is clicked: 6
#1 click: http://www.twitter.com
#2 click: http://www.facebook.com
#3 click: http://www.youtube.com
#4 click: http://www.google.com.sg
#5 click: Empty
#6 click: Empty

This are the codes I did so far.
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Might want to look up the javadoc for Stack.empty().
 
Scott Bee
Greenhorn
Posts: 16
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Joan -- I believe the issue is in line 33...


For the purposes of this loop, website and backbutton are fixed values. Only j and the state of myStack change as the loop progresses. So the expression website==backbutton gives the same result in each loop iteration.

If I were trying to solve this problem, I wouldn't use website or backbutton in the if statement. Instead, I would do a check to see if there are still any items left in the stack. Is there a method of the Stack class that returns true if the stack is empty, false otherwise?

--Scott
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't use java.util.Stack. Use this instead. Use the four methods you are used to from other stacks, viz push pop peek and there is one other whose name I am pretending to have forgotten. There is also a size method which allows you to see how many elements there are. Note that the two classes differ slightly in their Exception handling.

You will find your coding much easier if you get it out of the main method. Pushing popping and displaying should be done by separate methods.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott Bee wrote:. . . . . .
Another issue. The == operator is not like Victorian children. It should neither be seen nor heard.

And welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for my mistake; I hadn't noticed that website and backbutton are both primitives, and you can use == there. But they are hardly good names for numbers.
 
Jaypee Hernandez
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use array for my stacks and I clear the value by assigning a blank String into it. I haven't used Stacks class before.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaypee Hernandez wrote:I use array for my stacks and I clear the value by assigning a blank String into it. I haven't used Stacks class before.

In which case you must be using your own Stack class, because you've written:
  Stack myStack = new Stack();
at line 10.

Maybe you should show that to us as well.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!