This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Unexpected Recursion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Unexpected Recursion" Watch "Unexpected Recursion" New topic
Author

Unexpected Recursion

Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83


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?


SCJP 1.5 | SCWCD 5 | SCJP 6.0
[url]http://a2zjava.webs.com[/url] - Online training for Java/JSPs and Servlets/SCJP/SCWCD
http://soniyaahuja.webs.com
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

"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


Mohamed Sanaulla | My Blog
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
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

Joined: Sep 08, 2007
Posts: 3071
    
  33

Ah sorry, though I read it, didn't interpret it correctly.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

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().


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
@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!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unexpected Recursion