Java: thinking time

0 like 0 dislike
30 views
Greetings to all.
I've never written in Java, and here came the need to write the test system. Actually the only problem is that
1) you need to take a question
2) give the user five seconds to answer
3) if the user during this time entered a number, go immediately to the next question
4) if not entered, the program should understand that the answer was not given and go to the next question.
The answers I read Scanner'ohms, and I can't figure out how to get around that it blocks the flow of commands of the form next*() and hasNext*() until the newline character. Asking java professionals to help :)
by | 30 views

4 Answers

0 like 0 dislike
In a separate thread not make tried?
\rOn Java not writing, I passed
by
0 like 0 dislike
Try using a timer. By default it runs in a background thread.
\rdownload.oracle.com/javase/6/docs/api/java/util/Timer.html
by
0 like 0 dislike
In General while decided as follows:
\r
public static Integer readWithTimeout(int secs) { Integer answer_given = null; Scanner in = new Scanner(System.in); if (secs != 0) { secs *= 1000; BufferedReader inReader = new BufferedReader(new an InputStreamReader( System.in)); long time = System.currentTimeMillis(); try { while (System.currentTimeMillis() - time < secs) { if (inReader.ready()) { answer_given = in.nextInt(); break; } } } catch (IOException e) { e.printStackTrace(); } } else { answer_given = in.nextInt(); } return answer_given; } 

Another thing is that if during one of the question write the number, but don't add line breaks, and during one of the following questions then press Enter, the old text will be written as an answer to the current question. How to clear buffer from stale there characters?
by
0 like 0 dislike
Java had nothing to do with this particular console I / o.
With the same problem will encounter if you use scanf, fread, etc. in C++, PHP and many other languages.
If you want to get normal user interface, go directly to the GUI tulkita: AWT, Swing or SWT.
by

Related questions

0 like 0 dislike
5 answers
asked May 10, 2019 by alebul
0 like 0 dislike
4 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
28,757 users