2
System.out.println("Enter your age here:");
setAge(sc.nextInt());

How can I validate that users' age is not a char or a negative number? Ideally, if the users input anything but an int, the program would ask for the input again.

I have tried using a do-while, but doesn't seem to be working.

I am a beginner. Any help is super appreciated.

Thanks!

  • Does this answer your question? [Validating input using java.util.Scanner](https://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner) – azurefrog Dec 04 '19 at 19:41

2 Answers2

1

What you are doing with sc.nextInt() will only allow the user to enter an int or the program will throw an InputMismatchException (thus that part behaves the way you want). If you want to make sure the number isn't negative though, do this:

System.out.println("Enter your age here:");
while (!sc.hasNextInt()) {
    System.out.println("Please enter an integer.");
    sc.next();
}

int age = sc.nextInt();

if(age < 0) {
    //do what you want if the number is negative
    //if you're in a loop at this part of the program, 
    //you can use the continue keyword to jump back to the beginning of the loop and 
    //have the user input their age again. 
    //Just prompt them with a message like "invalid number entered try again" or something to that affect
}
else {
    setAge(age);
    //continue execution
}
Devon
  • 130
  • 10
  • Thank you! But what if the user inputs a char? – porschewitz Dec 04 '19 at 20:00
  • As I mentioned, the program will throw an `InputMismatchException` if someone enters a char instead of an int when you use `sc.nextInt()`. If you wanted the user input in the type of a string or char, you would use `sc.next()` or `sc.nextLine()`. – Devon Dec 04 '19 at 20:03
  • Still a bit confusing. I'm trying: `try { System.out.println("Enter your age: "); age=sc.nextInt(); } catch(InputMismatchException exception) { System.out.println("Please enter a valid number!"); age=sc.nextInt(); }` But still doesn't work – porschewitz Dec 04 '19 at 20:12
  • So the program says "Please enter a valid number!" if the user enters anything that's not a valid int. That should work. How is it behaving that you don't like what is occurring? What part isn't working? – Devon Dec 04 '19 at 20:14
  • After output Please enter a valid number! I get an InputMismatchException and it crashes without letting me input anything – porschewitz Dec 04 '19 at 20:19
  • Try doing this instead of the try catch, it might work better for you: `while (!sc.hasNextInt()) { System.out.println("Please enter a valid number!"); sc.next(); }` This will not allow the program to leave this loop until the user enters an int. – Devon Dec 04 '19 at 20:22
  • The `sc.next() ;` will help you get around the program throwing that exception by moving the scanner on to the next input, as `hasNextInt() doesn't move the scanner on itself. – Devon Dec 04 '19 at 20:27
  • `System.out.println("Enter your age:"); age = sc.nextInt(); while(!sc.hasNextInt()) { System.out.println("Please enter a valid number!"); sc.next(); }` I'm trying like this but still doesn't work... – porschewitz Dec 04 '19 at 20:31
  • Take a look at my answer now and try that while loop instead of using a try/catch. – Devon Dec 04 '19 at 20:32
0

The following block will do what you need:

int age;
System.out.println("Please enter an integer");
while (true) {
    try{
        age= scan.nextInt();
        if (age<=0) throw new Exception("Negative number");
        break;
    } catch(Exception e){
        System.out.println("Please enter a positive integer");
    }
    scan.nextLine();
}

// below just call 
setAge(age);

I hope this helps.

pdrersin
  • 276
  • 3
  • 10