Java.util.scanner error handeling

Posted by Hussain on Stack Overflow See other posts from Stack Overflow or by Hussain
Published on 2010-04-23T03:40:24Z Indexed on 2010/04/23 3:43 UTC
Read the original article Hit count: 312

Filed under:
|

I'm helping a friend with a java problem. However, we've hit a snag. We're using Java.Util.Scanner.nextInt() to get a number from the user, asking continiously if the user gives anything else. Only problem is, we can't figure out how to do the error handeling.

What we've tried:

do {
  int reloop = 0;
  try {
    number = nextInt(); 
  } catch (Exception e) {
    System.out.println ("Please enter a number!");
    reloop ++; }
  } while(reloop != 0);

Only problem is, this loops indefinatly if you enter in something not a number.

Any help?

© Stack Overflow or respective owner

Related posts about java

Related posts about scanner