Results 1 to 10 of 10
Thread: Stack problem
- 03-19-2010, 03:06 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 9
- Rep Power
- 0
Stack problem
Hi everyone. I am trying to write a simple method,which prints all of the elements of a stack.
import java.util.*;
public class Stackcalculator extends java.util.Stack
{
public void show(){
private LinkedList<Double> stack = new LinkedList<Double>();
double []b=new double[100];
int i=0;
while (stack.empty()==false){
b[i]=stack.pop();
System.out.println(""+b[i]);
i++;
}
}
}
The problem is that I get an error message saying that "method empty is not recognized" although the method is actually in the java class stack. Any ideas what I am doing wrong? I am using Bluej.
- 03-19-2010, 03:29 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Your stack variable is not a Stack, it's a LinkedList<Double>. LinkedList has no empty() method.
-Gary-
- 03-20-2010, 04:37 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 9
- Rep Power
- 0
Thanx. Any ideas how i can actually do it?
- 03-20-2010, 08:33 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
- 03-20-2010, 09:48 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
It's not at all clear what you're trying to do, but if it's really "print all the elements of a stack" (in which case, what are we trying to do with LinkedList<Double>?) then it would be good to take a look at the documentation for java.util.Stack. Hint: do you know how to print all the elements of a Vector?
-Gary-
- 03-20-2010, 10:46 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
- 03-20-2010, 10:51 AM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
- 03-20-2010, 11:21 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
The OP would be a good sport if s/he'd use a second stack like this:
kind regards,Java Code:while (!stack1 is empty) { T elem= pop from stack1 // <--- either print elem here push elem on stack2 } while (!stack2 is empty) { T elem= pop from stack2 // <--- or print elem here push elem on stack1 }
Jos
- 03-20-2010, 11:29 AM #9
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
- 03-20-2010, 12:03 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
recursion and call stack problem
By OptimusPrime in forum New To JavaReplies: 4Last Post: 12-26-2009, 09:49 PM -
Stack problem..pls help
By Mika in forum New To JavaReplies: 1Last Post: 02-16-2009, 08:10 AM -
Problem in Calculator implementation using Stack
By realahmed8 in forum New To JavaReplies: 1Last Post: 12-19-2008, 11:58 PM -
Stack class/problem in stackSize Initialization and usage of pop() function
By Mazharul in forum New To JavaReplies: 1Last Post: 11-17-2008, 09:32 AM -
Help with heap and stack
By coco in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 02:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks