Timer timer=new Timer();
TimerTask task=new TimerTask(){
@Override
public void run() {
if(isTimeCompleted){
System.out.println("Time over");
System.exit(0);
}
}
};
timer.schedule(task, 5000);
System.out.println("Type your word within 5 seconds..");
Scanner sc = new Scanner(System.in);
String text = sc.nextLine();
isTimeCompleted = false;
I have used above code to take input from user within 5 seconds but I have to hit enter after giving input wiithin 5 seconds. I don't know this should work like it would not require to press Enter if i have given input within 5 seconds .Any help will be appreciated. Thanks in advance.