Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

stacks

 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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.
 
Junilu Lacar
Bartender
Posts: 7557
52
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic