0

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);
    }   

}
Rajeev Atmakuri
  • 888
  • 1
  • 10
  • 22
  • Works fine for me. What input are you giving? – Nicholas K Nov 13 '18 at 18:28
  • `(0 > studentID) && (studentID > 999999)` will always be false. Are you sure about this check? – Rajeev Atmakuri Nov 13 '18 at 18:32
  • With the first testID method I could enter "Five" and hasNextInt() method would check to make sure it was an int and if not it would print out "Enter valid..." and then use nextLine() to reset the scanner. With the new one if I type in "Five" I get an inputMismatchException. – BamBamBondy Nov 13 '18 at 20:53

1 Answers1

1

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

StaticBeagle
  • 5,070
  • 2
  • 23
  • 34
  • Initially I declared public static int studentID; without ever initializing it and the first testID method worked. I only set studentID = -1; because I thought it would never enter the loop because of the default value being 0. I change it back to default value (0) and I am still getting an exception. – BamBamBondy Nov 13 '18 at 20:46
  • @BamBamBondy if `studentID` is set to `0` the condition will still fail because `studentID > 999999` will still be `false`. The problem is in the logic. For example, how do you go about setting the `studentID` in the second case? IOW, in the second case, there's no way for the user to set the `studentID`. In your first approach, you first verified that the input was an `int` and then moved on to check if that number is within the allowed range. The second case won't work "as is" for what you are trying to do. Case 1 was perfect though. – StaticBeagle Nov 13 '18 at 20:58
  • thanks. Wasn't thinking about how I verified the input and then set the variable in the first case. I will use the first method. – BamBamBondy Nov 13 '18 at 21:09