+ 1
Why scanner thows error inside a loop ? How to overcome that error?
When we try to take multiple input from user inside a loop and input varies from string to int to char on every iteration , program throws an exception . why ? and how to overcome those exception and run program smoothly . this happens when we get large no. of test cases in online coding and one ends up with runtime exception . I am really pissed of by this runtime exception known as Nosuchelementexception
4 odpowiedzi
+ 2
That exception comes when there was no input, yet input was expected.
On SoloLearn you have to do all inputs at once, otherwise you may get that error if you miss one input.
I suggest using an IDE like Intellij or Eclipse or Netbeans for multiple inputs with scanner.
+ 1
nosuchelementexception
0
What does the exception say?
0
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
Scanner has several methods that throw a NoSuchElementException.
Consider preceding the code that throws this exception with
while(scanner.hasNext()){
// note - or whatever you called scanner in your program :)
// do the thing that may throw the exception
}