Reputation:
I did almost everything to solve the annoying issue with "Long cannot be dereferenced", but anything worked out. Thus, can anyone please, help me? The problem is when I check if program timed out in if(System.currentTimeMillis().longValue()==finish)
, the comparison is not working.
public void play()
{
long begin = System.currentTimeMillis();
long finish = begin + 10*1000;
while (found<3 && System.currentTimeMillis() < finish) {
Command command = parser.getCommand();
processCommand(command);
}
if(System.currentTimeMillis().longValue()==finish){
if(found==1){System.out.println("Time is out. You found "+found+" item.");}
else if(found>1 && found<3){System.out.println("Time is out. You found "+found+" items.");}}
else{
if(found==1){System.out.println("Thank you for playing. You found "+found+" item.");}
else if(found>1 && found<3){System.out.println("Thank you for playing. You found "+found+" items.");}
else{System.out.println("Thank you for playing. Good bye.");}
}
}
Upvotes: 6
Views: 29001
Reputation: 131456
System.currentTimeMillis()
returns a primitive long
not a object Long
.
So you cannot invoke the longValue()
method or any method on it as primitive cannot be the object of method invocations.
Besides, it is useless to invoke longValue()
as System.currentTimeMillis() returns already a long value.
This is better :
if(System.currentTimeMillis()==finish){
But in fact this condition : if(System.currentTimeMillis()==finish)
could not be true
even if System.currentTimeMillis() == finish
in the while
statement :
while (found<3 && System.currentTimeMillis() < finish) {
Command command = parser.getCommand();
processCommand(command);
}
Because between the end of the while statement and the condition evaluation :
if(System.currentTimeMillis() == finish)
, the time goes on elapsing.
So you should rather use :
if(System.currentTimeMillis() >= finish){
Upvotes: 4