Don't know weather the is the correct place for this question
I want to read user input on a timer.
so input = myClass.getInput()
will equal to a read in input from the user (from the console)
or if the user does not respond in 2 seconds will return "" (or throw an exeption that can be caught)
I have tried to implement this using Threads (Timer and InputReader) and a monitor class but I can not get control flow to continue until the seconds are up regardless of weather a value is returned or not. Also my solution is very 'messy' and any solution would have to be 'neat' and clear.
Any help would be much appreciated
Also any solutions that don't use threads would be really great
Thanks
John
P.S This the current solution but it doesn't work properly as described above
(sourced from http://stackoverflow.com/questions/6...from-system-in)
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class MyThreadTest { public static void main(String[] args) { MyThread myThread = new MyThread(); myThread.start(); try { Thread.sleep(2000); } catch(InterruptedException e) { //Do nothing } myThread.interrupt(); } private static class MyThread extends Thread { @Override public void run() { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String msg; while(!isInterrupted()) { try { if(stdin.ready()) { msg = stdin.readLine(); System.out.println("Got: " + msg); } } catch(IOException e) { e.printStackTrace(); } } System.out.println("Aborted."); } } }