• Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginner: The catch statement is working in the loop but I cannot enter a new int with .nextInt.  RSS feed

 
Hans Hovan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm making a small program just to practice various Java exercises. It works fine if I enter the correct variable types. I want to catch when a user enters the wrong kind of input. In this chunk of code though, if the user enters a String when prompted for an int the program just loops continuously and does not give me the option to enter another input even though I am using .nextInt(). Any ideas or can someone point me in the right direction?



The output loops like this:

Menu:
1: Palindrome
2: Fibonacci
Enter your choice:
java.util.InputMismatchException
Please enter an integer choice.
Menu:
1: Palindrome
2: Fibonacci
Enter your choice:
java.util.InputMismatchException
Please enter an integer choice.

The loop appears to be working fine but I am not getting a chance to enter another choice. My choice seems to be stuck on whatever I first entered.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a Scanner throws an Exception it does not remove the token that caused the Exception from its input. If nextInt throws an exception then repeated calls to nextInt will also throw an Exception. It won't block waiting for more input because it already has input.

You need to clear the invalid token from the input by calling next() in the catch block.
 
Hans Hovan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:When a Scanner throws an Exception it does not remove the token that caused the Exception from its input. If nextInt throws an exception then repeated calls to nextInt will also throw an Exception. It won't block waiting for more input because it already has input.

You need to clear the invalid token from the input by calling next() in the catch block.


Ah, I see. Thank you very much with helping me figure that out.
 
Liutauras Vilda
Sheriff
Posts: 4922
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hans, did you mistype line 1?
"package" statement doesn't look correct.

Addition:
Nevertheless, package doesn't fit to a Java naming conventions. Package statement convention differs from classes, methods, variables naming conventions. Different words should be separated by underscore.
At least it should look like: "package java_problems;". Remeber, that package names starting with java or javax are reserved for Java libraries, so you shouldn't use them: "package java.problems;" <- wouldn't be allowed.

Also, your finally block is empty, is it your intention to leave like this?
Closing scanner, which pointing to "System.in" is not recommended also, you could search for older threads in this forum for information why.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!