This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Limiting time for user response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Limiting time for user response" Watch "Limiting time for user response" New topic
Author

Limiting time for user response

Dishant Verma
Greenhorn

Joined: Feb 09, 2012
Posts: 27

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.
Viktor Pergjoka
Greenhorn

Joined: Aug 30, 2010
Posts: 16
Maybe you could make something like this:


John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3435
    
  47

John Jai wrote:Also please UseAMeaningfulSubjectLine.

Good point. For now, I've changed the subject myself, since the old one was very cryptic.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

you could also use a Timer


SCJP
Visit my download page
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 498
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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Dishant Verma
Greenhorn

Joined: Feb 09, 2012
Posts: 27

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.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Limiting time for user response
 
Similar Threads
Can't a switch use final parameters?
Unable to Return Variable
Phrase Guessing Game Help
Help with number guessing game
Do while error, I'm going crazy