• 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

stacks

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic