Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unexpected Recursion

 
Soniya Ahuja
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the code above, if I enter the following input

An InputMismatchException is thrown but it gets thrown infinite times and is followed by 1. I cannot understand that why does int x = sc.nextInt() throw an exception again and again. Is it because something goes wrong with the scanner itself?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"ten" is not same as entering- 10. nextInt() expects 10 and not "ten".

And as far as the infinite loop is considered- success never becomes true, because there's an exception thrown, catch block tackles it and the loop continues. You can step through a debugger to check the behavior of the program
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:"ten" is not same as entering- 10. nextInt() expects 10 and not "ten".

And as far as the infinite loop is considered- success never becomes true, because there's an exception thrown, catch block tackles it and the loop continues. You can step through a debugger to check the behavior of the program


Hello Mohamed, well thanks for your reply but the problem is that I have entered ten on purpose. Haven't I mentioned that I get an InputMismatchException? Thanks for trying, but I already know the difference between ten and 10. Please read the entire question. Thanks again.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah sorry, though I read it, didn't interpret it correctly.
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is you don't consume the input. The user enters "ten". Then you call nextInt() but there is no int, just "ten". So the exception is caught and the loop starts all over. However, the "ten" is still there. So nextInt() doesn't read new input but reads that same "ten".

To fix this, simply call sc.next() in the exception handler to consume the "ten". You don't need to assign the return value, just consume it.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This does not have anything to do with recursion.

When Scanner.nextInt() encounters something that it cannot parse as an integer, it will simply read nothing from the scanner. So the text that the scanner read is staying there, and when you call nextInt() again it will try to parse the same input again. Your program has a loop, so when you enter an invalid input it will again and again try to parse the same thing that you already entered - and every time it goes wrong in the same way, leading to the same error again and again.

To avoid this, you could read the input using Scanner.nextLine(), and then parse that with Integer.parseInt().
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rob and Jesper: Thanks for the comments guys. I did figure it out that there was some problem once an incorrect datum was entered. To go around it, I was instantiating the Scanner in the try block, but that obviously meant creating a lot of new objects. Now with what you've explained, I can come up with a better solution, probably call next from the catch block. Thanks guys!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic