• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

reverse for stacks??

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heres my code


Heres what I have so far for testing:



My problem is how do I reverseStack?

this is what i tried:


these are the errors:
StackClass1.java:177: non-static method isEmptyStack() cannot be referenced from a static context
if ( !isEmptyStack())
^
StackClass1.java:178: non-static method pop() cannot be referenced from a static context
otherStack.push(pop());
^
StackClass1.java:178: 'void' type not allowed here
otherStack.push(pop());
^
3 errors
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

StackClass1.java:177: non-static method isEmptyStack() cannot be referenced from a static context
if ( !isEmptyStack())
^
StackClass1.java:178: non-static method pop() cannot be referenced from a static context
otherStack.push(pop());
^
StackClass1.java:178: 'void' type not allowed here
otherStack.push(pop());
^
3 errors



The first two errors mean that you are trying to call an instance method on your class, but you are calling it from the main method or another static method in the class.

In the third case, your pop() method has return type void. I expect you meant to make it something else.
[ March 20, 2007: Message edited by: Keith Lynn ]
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error tells you exactly what the problem is; you are trying to call a non static method from a static method.

The reverse method appears to be static static.
 
Shananne DuFrame
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it! In a loop I had to assign top() to a new variable, use the top() method to read the element, then pop() to get to the next element. Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic