I'm beginner to Java and I have been trying to solve this timer thing for like 3-4 hours. Have tried almost everything on the internet.
The thing is that the program should give user the option to type anything to start a new game or wait 10 seconds and the he will be redirected to menu.
This is how my code looks like:
long startTime = System.currentTimeMillis();
long maxDurationInMilliseconds = 10000;
while (System.currentTimeMillis() < startTime + maxDurationInMilliseconds) {
Scanner end = new Scanner (System.in);
System.out.println("Enter anything if you want to start a new game or wait 10 seconds and you will be redirected to the Menu");
String value;
value = end.nextLine();
if (value != null) {
playGame();
}
else if (System.currentTimeMillis() > startTime + maxDurationInMilliseconds) {
// stop running early
showMainMenu();
break;
}
}
But for some reason I can't get it to work, have been struggling to get this to work and stackoverflow is my last chance.
EDIT: Thank You everyone for your reply. Haven't fixed yet, getting headache from this and it's 03:31 AM.