how to do this?
how can i limit the time that the user can give input to the Scanner in this code? i mean that when 25 second (25000 millis) are over the user can't give input anymore and the game ends (program ends by writting: return;). you cant run the code on SoloLearn... (won't work). https://code.sololearn.com/ce0vyM1ZGkR8 writen code: import java.util.*; public class Main { public static void main(String[] args) throws InterruptedException { String[] randomWords = {"run" , "block" , "java" , "code" , "time"}; Random r = new Random(); String myRandomWord = randomWords[r.nextInt(5)]; System.out.println("In 10 seconds There will be a countDown, the system will give you a word and you will need to type it as fast as you can!"); Thread.sleep(10000); System.out.println("3"); Thread.sleep(1000); System.out.println("2"); Thread.sleep(1000); System.out.println("1"); Thread.sleep(1000); System.out.println("GO!!!"); System.out.println("Your word is : " + myRandomWord); long countTimeStart = System.currentTimeMillis(); Scanner s = new Scanner (System.in); String myWord = s.next(); long countTimeEnd = System.currentTimeMillis(); float timeToWriteWordInSeconds = (countTimeEnd - countTimeStart) / 1000; //1 sec = 1000 millis. float timeToWriteWordInMillis = countTimeEnd - countTimeStart; if (!myWord.equals (myRandomWord)){ System.out.println("You Lost!"); }else if (myWord.equals (myRandomWord) && timeToWriteWordInMillis <= 10000){ System.out.println("You Won!"); }else if (myWord.equals (myRandomWord) && timeToWriteWordInMillis > 10000){ System.out.println("You got the word right, but it took you to long! - you lost!"); } System.out.println(""); System.out.println(timeToWriteWordInMillis + " millis"); System.out.println("about " + timeToWriteWordInSeconds + " seconds");