Search code examples
javainputmismatchexception

Why am I getting inputMismatchException even when using hasNextInt() method?


This program asks the user to input their student id number which must be an integer between 0-999999. The testID method that has been commented out uses a do while loop with an inner while loop that ensures only integer values are input by the user. This method works without any issues. While trying to rewrite the code (second testID method) every time I run the program and type in a string or char value I get an inputMismatchException. This doesn't happen with the first method. Can someone please explain why this is happening?

import java.util.*;
public class StudentID{

    public static int studentID= -1;
    public static Scanner input = new Scanner(System.in);

    public static void main(String[] args){
        testID();
    }

    /*
    public static void testID(){
        System.out.println("Enter your Student EMPLID (0-999999):");
        do{
            while (!input.hasNextInt()){
                input.nextLine();
                System.out.println("Enter a valid Student EMPLID (0-999999).");
            }
            studentID = input.nextInt();
            if(0 > studentID || studentID > 999999){
                input.nextLine();
                System.out.println("Enter a valid Student EMPLID (0-999999).");
            }
        } while (0 > studentID || studentID > 999999);
        System.out.println("Student EMPLID: " + studentID);
    }

    */

    public static void testID(){
        System.out.println("Enter your Student EMPLID (0-999999:)");
        while ((!input.hasNextInt()) && (0 > studentID) && (studentID > 999999)){
            input.nextLine();
            System.out.println("Enter a valid Student EMPLID (0-999999:)");
        }
        studentID = input.nextInt();
        System.out.println("Student EMPLID: " + studentID);
    }   

}

Solution

  • The problem is in the logic inside the while. In the commented testID() method, you checked the following condition to be true:

    while(!input.hasNextInt()) {
        ....
    }
    

    Thus for a non integer input, input.hasNextInt() will return false and !hasNextInput() will consequently return true and the while will keep on looping till a valid integer was entered.

    Now in case 2, the condition inside the while is always false,

    while(!input.hasNextInt()) && (0 > studentID) && (studentID > 999999) {
        ...
    }
    

    See, here studentID is set by default to -1 thus even though !input.hasNextInt() returned true as expected, the result of anding this true with (studentID > 999999) is false. So the code will never go into the while loop and move on to the next line which happens to be,

    studentID = input.nextInt();
    

    This will throw a InputMismatchException since the value entered was non an integer