I'm working on a text adventure game for my Java class, and I'm running into a problem while trying to time a print statement from showing up in the console.
Basically after 45 seconds I would like a print statement to show up, in this case the print statement would be reminding the user that they need to let their virtual dog out...
I also need the timer to reset after the user gives the correct command.
import java.util.Timer;
import java.util.TimerTask;
...
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("delayed hello world");
}
}, 45000);
Timer
TimerTask
To cancel the timer, either use a TimerTask variable to remember the task and then call its cancel() method, or use timer.purge()
; the latter cancels all tasks on the timer. To schedule the task again, just repeat.
You'll probably want to do more advanced operations in the future, so reading the Timer API docs is a good idea.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With