+ 1

How do you print out something if user hasn't entered Any data in Scanner

31st Dec 2016, 9:46 PM
daragh
2 Respuestas
+ 1
After reading line, you can check the value of the input variable with and if, so you print something if the value is null
1st Jan 2017, 12:56 AM
Dago VF
Dago VF - avatar
0
boolean BufferedReader.ready() returns true if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block. long waitTimeout = 30 * 1000; //30 Seconds long startTime = System.currentTimeMillis(); InputStreamReader fileInputStream = new InputStreamReader(System.in); BufferedReader bufferedReader = new BufferedReader(fileInputStream); while ((System.currentTimeMillis() < (startTime+timeout))) { if (bufferedReader.ready()) { System.out.println(bufferedReader.readLine()); } } System.out.println("Waited for input for "+timeout/1000.0+" seconds. \nRequest TImed Out");
3rd Jun 2017, 10:27 PM
Tom Cox
Tom Cox - avatar