0

I'm having an issue with Scanner in that it seems to be taking the input value type and forcing the next time the user inputs a value to be of the same type. I cannot find any reason why this code is not working and gives me an InputMismatchException as I have written code like this a million times and not had problems.

 public void register(){
    Scanner input=new Scanner(System.in);
        System.out.println("What course would you like to register for?");
        String course_name = input.next();
        System.out.println("What section?");
        int section = input.nextInt();

        for (int i = 0; i < courses.size(); i++) {
            if (courses.get(i).getCourse_name().equals(course_name)) {
                if (courses.get(i).getCourse_section() == section) {
                    courses.get(i).AddStudent(this.first_name+" "+this.last_name);
                }
            }
        }
        input.close();
    }

This problem is not just for the register() method, but program-wide, eg with this code:

public void Options() {
    Scanner input=new Scanner(System.in);
    while (true) {
        System.out.println("What would you like to do (Enter corresponding number):" + "\n" + "1) View all courses" + "\n" + "2) View all courses that are not full" + "\n" + "3) Register on a course" + "\n" + "4) Withdraw from a course" + "\n" + "5) View all courses that the current student is being registered in" + "\n" + "6) Exit");
        int user = input.nextInt();
        if (user == 1)
            viewAll();
        if (user == 2)
            viewAllOpen();
        if (user == 3)
            register();
        if (user == 4)
            withdraw();
        if (user == 5)
            viewRegistered();
        if (user == 6) {
            Serialize();
            break;
        }
    }

If one of the methods, such as register required the user to enter a String, the int user=input.nextInt(); will cause an InputMismatchException.

  • 1
    Check http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo – TheLostMind Feb 17 '17 at 07:44

1 Answers1

0

I've reproduced this code and I am not having the same issue. If the user enters an integer such as 11 when prompted for the course number the code will run fine. Of course, if you enter something that is not an integer it will throw an InputMismatchException. Refer to the Java doc description for Scanner#nextInt(), in particular this:

Scans the next token of the input as an int.

An invocation of this method of the form nextInt() behaves in exactly the same way as the invocation nextInt(radix), where radix is the default radix of this scanner.

Throws:

InputMismatchException - if the next token does not match the Integer regular expression, or is out of range

Read More

If you want to prevent this and do not want to deal with a try-catch you could pause execution until a valid integer is give.

public static void register(){
    Scanner input=new Scanner(System.in);
    System.out.println("What course would you like to register for?");
    String course_name = input.next();
    System.out.println("What section?");
    //Loop until the next value is a valid integer.
    while(!input.hasNextInt()){
        input.next();
        System.out.println("Invalid class number! Please enter an Integer.");
    }
    int section = input.nextInt();
    input.close();
        
    System.out.println(course_name + " " + section);
}
Community
  • 1
  • 1
d_scalzi
  • 356
  • 2
  • 9
  • It's not an issue of not entering an integer. When I run it, I don't even get the chance to enter an integer as the InputMismatchException occurs immediately after I enter the name of the course I would like to register for and the program stops. – UnionSquareBanter Feb 17 '17 at 08:02
  • Try closing the scanner in the Options method as soon as you get your scan your requested string. The fact that the resource is still open my interfere with the scanner being opened in the register method. If that doesnt work then paste your stack trace. `Scanner input=new Scanner(System.in); while (true) { System.out.println("blah"); int user = input.nextInt(); input.close() //Other code here.` – d_scalzi Feb 17 '17 at 08:06