Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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.
 
I can't beleive you just said that. Now I need to calm down with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic