0

My code crashes whenever the user inputs letters. I want to make it so the user can only input whole numbers under 18. If it's under 18 I want to prompt them to "enter a number under 18" and if its a letter prompt them "only enter whole numbers".

I got the under 18 working and above 18 working. I have tried while loops for the letters but it still would crash.

static void intMethod (String vName) {
        Scanner intInput = new Scanner(System.in);
        System.out.print("Enter "+ vName + ": ");
        intStore = intInput.nextInt();
        while(tf) {
        if (intStore < 18 ) {
        tf = false;
        }else if(intStore >= 18) {
                 System.out.println("Please only enter a whole number under 18");
                 System.out.print("Enter an " + vName + ": ");
            intStore = intInput.nextInt();
        }else if(!intInput.hasNextInt()){
             System.out.println("Please only enter a whole number under 18");
             System.out.print("Enter an " + vName + ": ");
             intInput.nextLine();    
        }
}
}

Whenever I try to input letters I get this error message in stacktrace:

Enter age: af
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at IlstueduClass.FunWithMethods.intMethod(FunWithMethods.java:27)
    at IlstueduClass.FunWithMethods.main(FunWithMethods.java:56)
GhostCat
  • 127,190
  • 21
  • 146
  • 218
  • You are explicitly asking the scanner to read an integer when you write intStore = intInput.nextInt(); Use the method next() instead then parse the returned string. – Islam Muhammad Sep 12 '19 at 15:32
  • Possible duplicate of [Validating input using java.util.Scanner](https://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner) – Tom Sep 12 '19 at 15:33
  • It is unclear what you are asking. What do you want the program to do when the user tries to input a non-integer token? – arcadeblast77 Sep 12 '19 at 15:34
  • 2
    Hint: please properly format/indent all your code. Indentation really matters, to understand context, and nesting levels of blocks. ALso note: you never change "tf" to true. Btw: tf is a nothing saying name. Why not use a name that says what the variable is supposed to be used for? – GhostCat Sep 12 '19 at 15:34

2 Answers2

1

I would solve this by storing the response to a String and attempting to parse it to an Integer.

String response;
Scanner intInput = new Scanner(System.in);
System.out.print("Enter "+ vName + ": ");
response = intInput.next();
try {
intStore = Integer.parseInt( response );
}
catch (NumberFormatException e) {
    System.out.println("Please enter a whole number only");
}
Deane Kane
  • 116
  • 1
  • 13
0

The nextInt() is excepted to throw an InputMismatchException if it does not receive a number. You can use a try catch block to check if this exception is thrown.

Scanner sc = new Scanner(System.in);
try {
    Integer input = sc.nextInt();
    //It is a number
}
catch (InputMismatchException ex) {
    //It is not a number
}

(...)
Jose Nuno
  • 507
  • 2
  • 8