Can anyone explain me when do I get a "java.util.NoSuchElementException" while using Scanner class ?
I was practicing few Scanner examples, and I am getting the above exception many times. The following is one example. But I am not able to figure the reason out. Please help.
The following is the output of the above code block :
Abc
7
false
3
Exception in
thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at ScanTest.main(ScanTest.java:27)
Its clear that the program contains int values in the
String and the Scanner is able to identify that, thats why it prints the values in the output above. But when i say int a=scanner.nextInt(); it throws the exception.
Please explain.