I want to write infinity while statement which will perform every time when user just presses Enter. Every stream readers I know perform when user enteres anything into console or presses Enter twice.
I want to write something like this:
Console:
output 1 [enter have been pressed onse]
output 2 [enter have been pressed onse]
output 3 [enter have been pressed onse]
[so on…]
This is my current code:
addQuestionsInArray();
Random r = new Random();
DataInputStream is = new DataInputStream(System.in);
while (true) {
String question = questionsList.get(r.nextInt(questionsList.size()));
System.out.println(question);
if (String.valueOf(is.readInt()).equals("0")) {
break;
}
}
To wait for the user to hit enter, I usually do something like
Demo:
Output:
Regarding your edit, I still suggest you use a
Scanner. (DataInputStreamshould be used for binary data, not strings and characters written onSystem.in):