• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Having problem with stacks

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 9734
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Might want to look up the javadoc for Stack.empty().
 
Greenhorn
Posts: 16
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76860
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76860
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76860
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic