+ 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

31st Jul 2017, 2:19 PM
Samrat
Samrat - avatar
4 Respuestas
+ 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.
31st Jul 2017, 6:49 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
nosuchelementexception
31st Jul 2017, 5:38 PM
Samrat
Samrat - avatar
0
What does the exception say?
31st Jul 2017, 4:10 PM
marit vandijk
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 }
31st Jul 2017, 7:36 PM
marit vandijk