File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes reverse for stacks?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reverse for stacks??" Watch "reverse for stacks??" New topic
Author

reverse for stacks??

Shananne DuFrame
Ranch Hand

Joined: Nov 13, 2006
Posts: 51
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jan 01, 2007
Posts: 333
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

Joined: Nov 13, 2006
Posts: 51
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: reverse for stacks??
 
Similar Threads
get variable info from subclass
Selecting characters from a string
Stacks using Arrays
Picking characters from a string
toString