1

I am a novice Java student and am trying to complete a program that uses the scanner to input 5 students' names, and then a loop within to get 3 grades for each student. I am stuck as I keep getting an Input Mismatch error and I don't know why. I have tried to correctly match what kinds of input are coming in to the variables. Any help would be greatly appreciated!

This is what I have:

import java.util.Scanner;

public class StudentGrades {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    // Declare arrays, grades to hold [student #][course] and correspond to the grade. studentNames to be paralell and hold the names.
    int [][] grades;
    grades = new int[5][3];
    String[] studentNames = new String[5];
    int studentNumber = 0;
    int courseNumber = 0;

  // Create loops to put values in both arrays, using student# as a counter
  if (studentNumber < 5) {
    System.out.println("Enter the student name");
    studentNames[studentNumber] = input.next();
    // Nested loop to enter the grades
    if (courseNumber < 5) {
      System.out.println(" Enter a grade for " + studentNames[studentNumber]+" for course #" + courseNumber);
      grades[studentNumber][courseNumber] = input.nextInt();
      courseNumber = courseNumber + 1;
    }
  studentNumber = studentNumber + 1;
    }

  }

}

And this is what I get:

Exception at thread "main" java.util.InputMismatchException
at java.util.Scanner.throwfor{Scanner.java:909}
at java.util.Scanner.next{Scanner.java:1530}
at java.util.Scanner.nextInt{Scanner.java:2160}
at java.util.Scanner.nextInt{Scanner.java:2119}
at StudentGrades.main{StudentGrades.java:20}
0

3 Answers 3

1

Your problem is in line 20.

grades[studentNumber][courseNumber] = input.nextInt();

that means that in the input, it is expecting an int, but it founds another thing, like a double, a char array or anything else

There is also another problem, you declare your grades as:

grades = new int[5][3];

the last number means that you can access to grades from [0..4][0..2]

but your if statement:

if (courseNumber < 5) 

means that you will access to a number higher than '2' in

grades[studentNumber][courseNumber] = input.nextInt();

which will raise an OutOfBoundsException

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks, I fixed the course number. It should only go to three. But I still don't know how to fix the problem in line 20. It is supposed to prompt me to enter the integer, but it doesn't let me get that far.
That would be because you already entered more information from input than needed and it gets it. Can you show an input example?
Yes, I am entering a string for the first input, the student name.
System.out.println("Enter the student name"); studentNames[studentNumber] = input.next();
What I'm asking for is for the input itself, not a code of the input ;)
|
0

From the docs:

Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

From your stack trace:

Exception at thread "main" java.util.InputMismatchException
at java.util.Scanner.throwfor{Scanner.java:909}
at java.util.Scanner.next{Scanner.java:1530}
at java.util.Scanner.nextInt{Scanner.java:2160}
at java.util.Scanner.nextInt{Scanner.java:2119}
at StudentGrades.main{StudentGrades.java:20}

the Exception is being thrown by your call to nextInt.

Thus, you're getting an exception because you're requesting an integer and the Scanner is finding something that's not an integer.

Comments

0

Yes, like others have suggested, your problem in in the line:

grades[studentNumber][courseNumber] = input.nextInt();

Because your input is not recognised as an integer.

You should also be aware that your code will not loop through five times, it will go through once and exit since if statements do not repeat.

To loop you should probably use a for loop, something along these lines:

for(int i = 0; i < 5; i++){
    //You code should be the same in here
}

Or change your if's to while.

3 Comments

Thanks. I will try the that for loop.
Still unsure of how to fix the input. I am using a string first, as it is asking for student name. Then, I am looking to have it ask for a grade, which would be an integer, it just doesn't let me get that far
Try using String s = input.next(); Integer.parseInt(s); This will hopefully work. You could compress it to just one line: grades[studentNumber][courseNumber] = Integer.parseInt(input.next());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.