[Logo]
Forums Register Login
Limiting time for user response
Hello Everyone,




I just want user to enter number "yourguess" with in 10seconds.

If he enters in 10 sec, then the timer must be stopped and it should jump out of the Outer and continue with rest of the code.
And the same should be continued 10times.
If he does n't enters in 10 sec then the game should be over.

Thanks in advance.
Maybe you could make something like this:


You can make the thread sleep for 10 seconds and check if user has input an integer using hasNextInt() method (At least a dummy enter is required from the user if he is not entering anything).

Also please UseAMeaningfulSubjectLine.
 
John Jai wrote:Also please UseAMeaningfulSubjectLine.

Good point. For now, I've changed the subject myself, since the old one was very cryptic.
you could also use a Timer
 
John Jai wrote:You can make the thread sleep for 10 seconds and check if user has input an integer using hasNextInt() method.


I wouldn't do that, and I'm not even sure it would work. You don't want the thread accepting user input to sleep.
 
Jeff Verdegan wrote:
John Jai wrote:You can make the thread sleep for 10 seconds and check if user has input an integer using hasNextInt() method.


I wouldn't do that, and I'm not even sure it would work. You don't want the thread accepting user input to sleep.


What is wrong with this approach? This is how I see it working.

Application asks user to enter number. It waits for given time for user to enter number. Waiting time is implemented using thread. It sleeps for given time at MAX. Also, the thread can be awaken by user action like number is entered.
 
Saurabh Pillai wrote:
Jeff Verdegan wrote:
John Jai wrote:You can make the thread sleep for 10 seconds and check if user has input an integer using hasNextInt() method.


I wouldn't do that, and I'm not even sure it would work. You don't want the thread accepting user input to sleep.


What is wrong with this approach? This is how I see it working.

Application asks user to enter number. It waits for given time for user to enter number. Waiting time is implemented using thread. It sleeps for given time at MAX. Also, the thread can be awaken by user action like number is entered.


Yes, if you have a separate thread doing the sleeping, that's fine. I read John Jai's post as having the thread that's reading the input do the sleeping, which makes no sense. Perhaps I misunderstood.
Thanks for updating the subject line. And thanks everyone, appreciating your response...

I want :

-> user to enter a number(yourguess) with in 10seconds and that count down should be displayed...

-> when user enters the number(yourguess) count down must be stopped and asks for another attempt, if the number(yourguess) doesn't match up with the number to guess i.e

-> If he makes the final(10th) attempt then output should be
- (if correct) "You guessed the RIGHT NUMBER! The number was: " +numberToGuess.
- (if incorrect) "Sorry, you didn't guess the right number. The secret number was " + numberToGuess.

 
Dishant Verma wrote:Thanks for updating the subject line. And thanks everyone, appreciating your response...

I want :

-> user to enter a number(yourguess) with in 10seconds and that count down should be displayed...

-> when user enters the number(yourguess) count down must be stopped


Doing this in a console app will not be easy. Consoles are best suited for alternating input/response.

The simplest approach for what you're asking would be to have one thread print out the countdown, a line at a time, and another thread read the input. But that would be a lousy user experience. If the user is typing while the next second ticks over, his response will be split across lines and intermingled with the countdown output.

To get an output where there's a single line, and the countdown is updating while the user is thinking and typing, you'd have to use something like JavaCurses. I've never used that, and I don't know how well it works, how well supported it is, how portable it is, or how easy to use it is.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Nov 21, 2017 16:29:21.