• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unexpected Recursion

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah sorry, though I read it, didn't interpret it correctly.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic