1

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.

2 Answers2

1

Using TimerTask(http://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html):

public class MyClass {
    private static final int TEN_SECONDS = 10000;
    private String userInput = "";

    TimerTask timerTask = new TimerTask(){
        public void run() {
            if(userInput.equals(""))
                showMainMenu();
        }
    };

    public void getInput() throws Exception {
        Timer timer = new Timer();
        timer.schedule(timerTask, TEN_SECONDS);

        System.out.println("Press any key or wait 10 seconds to be redirected to the Menu.");
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        userInput = in.readLine();

        timer.cancel();
        if (!userInput.equals(""))
            playGame();
    }

    public static void main(String[] args) {
        try {
            (new MyClass()).getInput();
        } catch( Exception e ){
            System.out.println(e.getMessage());
        }
    }
}
Community
  • 1
  • 1
iamreptar
  • 1,461
  • 16
  • 29
  • That doesn't work. `hasNext()` doesn't return until there is something in the input buffer. – tbodt Oct 21 '13 at 01:00
0
//make this booean part of the class and not function
Boolean isStopped = false;    

System.out.println("Enter anything to start new game.");
Scanner end = new Scanner (System.in);

final Thread startThread = new Thread(new Runnable(){
    public void run(){
        try{
            Thread.sleep(10000);
            if(!isStopped)
                showMenu();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
});
final Thread inputThread = new Thread(new Runnable(){
    public void run(){
        end.nextLine();
        isStopped = true;
        startGame();
    }
});
inputThread.start();
startThread.start();
john01dav
  • 95
  • 1
  • 4
  • `stop` is deprecated and *"is inherently unsafe"* - See [Java Thread Primitive Deprecation](http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html) for more details... – MadProgrammer Oct 21 '13 at 01:11
  • Ok, it is fixed. Thank you for letting me know! – john01dav Oct 21 '13 at 01:13
  • This is going to leave the `inputThread` blocked at the `end.nextLine` method indefinitely. It might even effect future input functionality... – MadProgrammer Oct 21 '13 at 01:19
  • @iamreptar It's still going to leave the `WaitForInput` thread running and the `Scanner` attached to the `System.in`, which "may" cause problems with future interactions with `System.in`. It could actually execute `playGame` twice ;) – MadProgrammer Oct 21 '13 at 01:33
  • So, we would need to synchronize the Scanner in order to use it in multiple threads? – iamreptar Oct 21 '13 at 01:45