• Post Reply Bookmark Topic Watch Topic
  • New Topic

Allowing a user to give input only within certain time  RSS feed

 
shashank shashi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to make a simple terminal quiz game,where the user will be allowed to enter the answer within 10 seconds,else the question will pass on.But I couldn't figure out how do allow user to give input within certain time.Thanks for any help.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you familiar with the classes which allow you to get a time from your PC? You can read about them in the Java™ Tutorials.
Did you know the System class has two methods allowing you to get times from the PC?
 
shashank shashi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am familiar with currentTimeMillis() and nanoTime(), I tried this.

But the problem,if user wont give the input the condition can't be checked(As the execution will be stuce at 'n=u_in.nextInt()').
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Not sure if this will work exactly, but you can try using hasNextInt() instead of nextInt() -- meaning check if the int is available instead of trying to read it. You still have two issues though.

If the user doesn't enter an int, you need to check that (probably with hasNext()) and clear it. Also, standard input needs a carriage return before the data is available, so in theory, the user has enter the number, but just hasn't press return yet.

Henry
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to do it is to use nextInt and calculate the time before and after; that way you an show an error message saying, “Sorry, more than 10″,” is the delay is too long.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess she/he is asking how to break from the blocking System.in.read after 10 seconds. You could probably do it with multiple Threads one thread reading from System.in and the main Thread waiting for 10 seconds on the Thread that waits for input. I remember doing something similar to timeout early on network I/O. Maybe others have simpler ideas.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A.J. Côté wrote:I guess she/he is asking how to break from the blocking System.in.read after 10 seconds. You could probably do it with multiple Threads one thread reading from System.in and the main Thread waiting for 10 seconds on the Thread that waits for input. I remember doing something similar to timeout early on network I/O. Maybe others have simpler ideas.


Yeah, I had a similar thought. Sadly, you can't interrupt System.in. Since you have to wait for the user to get ready for the next question anyway, you might as well just wait for them to enter an answer, and then check whether the answer was given in a timely fashion in the same thread.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:

Yeah, I had a similar thought. Sadly, you can't interrupt System.in. Since you have to wait for the user to get ready for the next question anyway, you might as well just wait for them to enter an answer, and then check whether the answer was given in a timely fashion in the same thread.


Yeah, since Thread.stop() has been deprecated, I can't think of any way other to kill the blocked Thread on reading form System.in. On network I/O, one could just let the blocked Thread timeout by itself and discard the result or exception.

I don't know, maybe with async io but I haven't got a clue.

Maybe make her/his quizz game client/server? ;-)

It might work using deprecated Thread.stop() although, not sure, maybe System.in wouldn't unblock even when the Thread is stopped...

Oh, maybe create your own InputStream that has System.in as a member and send <enter> to it programmatically. We would need to set a bounty for a solution ;-)

Interesting challenge.

 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A.J. Côté wrote:
Stephan van Hulst wrote:

Yeah, I had a similar thought. Sadly, you can't interrupt System.in. Since you have to wait for the user to get ready for the next question anyway, you might as well just wait for them to enter an answer, and then check whether the answer was given in a timely fashion in the same thread.


Yeah, since Thread.stop() has been deprecated, I can't think of any way other to kill the blocked Thread on reading form System.in. On network I/O, one could just let the blocked Thread timeout by itself and discard the result or exception.

I don't know, maybe with async io but I haven't got a clue.

Maybe make her/his quizz game client/server? ;-)

It might work using deprecated Thread.stop() although, not sure, maybe System.in wouldn't unblock even when the Thread is stopped...

Oh, maybe create your own InputStream that has System.in as a member and send <enter> to it programmatically. We would need to set a bounty for a solution ;-)

Interesting challenge.



fork a bash process with Runtime to read the value then use the kill command to kill it? hehehe...
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best solution is to simply use a user interface that was meant for concurrent interaction, such as a Swing GUI.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!