So I did a bit more research into what I wanted.
I want to time an input, meaning that I want to ask for an input, however, if there is no input after a specified amount of time, a task will perform. If there is an input in that specfied amount of time, a different task will perform.
I then want to loop this so every time a No input or input is specified, the code will then restart, asking for another input and it will restart for an infinite amount of time.
I tried to do this by converting my timed input into a method and looping it in the main method. However, a problem I discovered is after a few tries of alternate inputs and inputting just an enter key, multiple instances of the timertask will perform, resulting in an exception. Is there an easier way to do this? or is there a way to not allow multiple timertask instances to perform?
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class timerTestasMethod {
public static String input = "";
public static void main(String[] args) {
while (true)
{
timerInput();
}
}
public static void timerInput() {
TimerTask task = new TimerTask() {
public void run()
{
if (input.contains("")){
try {
System.out.println("No Input");
Thread.sleep(1000);
main(null);
} catch (Exception e) {
System.out.println("e");
}
}
}
};
Timer timer = new Timer();
timer.schedule(task, 10*500);
System.out.println("You wanna hear something?");
Scanner in = new Scanner(System.in);
input = in.next();
timer.cancel();
timer.purge();
try {
System.out.println("With Input");
Thread.sleep(500);
} catch (Exception e) {
System.out.println("e");
}
}
}