What will be the output of following quiz on reading input data using Scanner ?
The while loop iterates over some data and prints if it finds a Boolean value.
So, it prints true and false. But when it finds 0, the program never moves to next token since it is not Boolean.
The correct syntax to get only Boolean values would be :
if(sc.hasNextBoolean()) System.out.println(sc.nextBoolean()); else sc.next();
Correct answer is “true false followed by infinite loop”
© 2016, www.topjavatutorial.com. All rights reserved. On republishing this post, you must provide link to original post