wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes stacks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "stacks" Watch "stacks" New topic
Author

stacks

Mitch Krah
Ranch Hand

Joined: Sep 06, 2004
Posts: 41
I am trying to load a time stamp into a stack using the push method. However, when I try to push the .getTime onto the stack I get the following error:

cannot be applied to (long). So, I had to use Long (new *.getTime()).

Also, I want to give the amount of time the element was in the stack. So, when I pop I want to subtract the current time from the time that was input on the stack to get the time on the stack. This gives me an error, "operator - cannot be applied".

The following is my code.

Date now = new Date();
int n;
Stack S = new Stack();

n = getInput();


switch (n) {
case 1: S.push(new Long (now.getTime()));
n = getInput();
break;
case 2: if (S.isEmpty())
System.out.println("No sausage left in the tray");
System.out.println("You just got a sausage from the tray that is "

// here is where the trouble starts! - operator on S.pop()

+ (new Long (now.getTime()) - S.pop()) + " seconds old");
n = getInput();
break;

Any help/suggestions would be appreciated.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

The Stack.push() needs an Object and Stack.pop() returns an Object but you have to use the primitive values to do the calculation. Use Long.longValue() to get the primitive value for performing calculations.


Junilu - [How to Ask Questions] [How to Answer Questions]
Mitch Krah
Ranch Hand

Joined: Sep 06, 2004
Posts: 41
OK. I guess I am just not getting the syntax (I even looked up Java doc). I tried:

Long.longValue(S.pop());

But, I get "cannot resolve longValue method of java.lang.Long?

What am I missing here?

Thank you.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Mitch Krah:
Long.longValue(S.pop());
longValue() is an instance method of class Long. S.pop() will return the Long you pushed onto the stack, but the reference will be of type Object. You must cast the Object reference to a Long reference and then use longValue().
Mitch Krah
Ranch Hand

Joined: Sep 06, 2004
Posts: 41
Wow! That was not inherently obvious. I tried your suggestion and it worked great.

However, now I keep getting times of 0. I will have to do some debugging and check the values. But, thank you very much for your help.

Mitch
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
While it wouldn't change the fact that you'll need to use casting, you could store the Dates in the Stack instead of Longs. It really depends on your needs, but perhaps this will seem more readable?Of course, you could also skip the use of Date altogether.
[ March 13, 2005: Message edited by: David Harkness ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Mitch Krah:
Wow! That was not inherently obvious. I tried your suggestion and it worked great.

However, now I keep getting times of 0. I will have to do some debugging and check the values. But, thank you very much for your help.

Mitch


I strongly suggest that you follow the Collections Trail from Sun's Java Tutorial. It explains how these details work in the Collections Framwork.

Layne


Java API Documentation
The Java Tutorial
Mitch Krah
Ranch Hand

Joined: Sep 06, 2004
Posts: 41
Thank you for the suggestion. I tried to incorporate your suggestion, but ended up messing things up. I will try again later, after I see if I can get my current program to run.

Another question:

When I pick the option to push the getTime to the S stack, I notice that the same "date/time" is sent everytime to the stack I see in the debugger that something is being sent to the stack, but it is the same value everytime. Any ideas on why I am not getting current time each instance that I push? The following is the part of the code that pushes the "date/time":

public static void main(String[] args) {
Date now = new Date();
int n;
Stack S = new Stack();
long stackTime, currentTime, diff;

n = getInput();

while (n <= 4 && n > 0) {
switch (n) {
case 1: S.push(new Long (now.getTime()));

break;
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The problem is that the Date now is only being created once. At creation it is initialized with the current time. You should modify your code to create a new Date object each time the while loop iterates:


HTH

Layne
Mitch Krah
Ranch Hand

Joined: Sep 06, 2004
Posts: 41
OOPS! You are so right. The program is now working pretty good. I need to figure out how to convert the time I am getting into seconds and things should be better.

I will definately try your suggestions (when I get some time). Your ideas do appear to be much more LOGICAL, then what I did.

Thank you very much.

Also, I will bookmark the Collections tutorial and see if it helps.

Thank you again.

Mitch
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Mitch Krah:
I need to figure out how to convert the time I am getting into seconds and things should be better.
Since getTime() returns a number in milliseconds, simply divide by 1000 to convert it to seconds.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: stacks
 
Similar Threads
Date class - GeekWatch
What is the difference??
Stacks
read an expression and solve it...
Calendar Object to String