Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reverse for stacks??

 
Shananne DuFrame
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • 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 ]
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic