javaarraysfor-looptest-class

Scanner skips input in a for loop


So I have this code:

public class Subjects {
    String name;
    int period;
    char grade;

public void period()
{
    System.out.println("I have " + this.name + " during period " + this.period + ".");
}

public void study()
{
    if (this.grade == 'B')
    {
        System.out.println("I study for " +  this.name + ", so I get a B!");
    }
    else if (this.grade == 'A')
    {
        System.out.println("I study for " +  this.name + ", so I get an A!");
    }
    else
    {
        System.out.println("I don't study for " +  this.name + ", so I get a " + this.grade +". :(");
    }
}

}

And this test class:

import java.util.Scanner;

public class SubjectsTest {
    public static void main (String [] args)
{
    Scanner kboard = new Scanner(System.in);
    Subjects[] classes;

    System.out.print ("How many classes do you have? ");
    int x = kboard.nextInt();
    int y;
    classes = new Subjects[x];

    for (int b = 0; b < x; b++) 
    {
        classes[b] = new Subjects();
    }

    for (int a = 0; a < x; a++)
    {
        y = a + 1;
        System.out.println("Period " + y );

        System.out.println ("Enter the subject name: ");
        classes[a].name = kboard.nextLine();
        System.out.println ("Enter your class period: ");
        classes[a].period = kboard.nextInt();
        System.out.println ("Enter your grade in the class: ");
        classes[a].grade = kboard.next().charAt(0);


    }

    for (int i = 0; i < x; i++)
    {
        classes[i].period();
        classes[i].study();
    }


}

}

What should happen is the user puts in the number of classes they have (e.g. 8) and then puts in the name, period and their grade for each one. Then at the end, it prints 2 statements for each class.

However, when I run the program (in Eclipse), after it asks How many classes do you have? and the user answers, the system prints out the next two questions without waiting for an answer to the first. My error message looks like this:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at SubjectsTest.main(SubjectsTest.java:27)

Why does it do this? How can I fix it? I'm new to Java, so any help would be much appreciated!


Solution

  • You should put a kboard.nextLine(); after your kboard.nextInt(); call that gets the number of classes.

    This will read in the rest of the kboard.nextInt(); line and allow reading in your subject name to work properly. Currently, your kboard.nextLine(); to read in the subject name is reading in the remainder of your input for the number of classes. So when you're trying to read in the subject, it's actually waiting for the int for period and giving you that exception.

    EDIT: Sorry for all the edits, the accepted answer for this question might make a little more sense: Using scanner.nextLine()