Java input mismatch error using scanner

前端 未结 3 1502
时光取名叫无心
时光取名叫无心 2021-01-26 19:28

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

3条回答
  •  滥情空心
    2021-01-26 19:34

    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

提交回复
热议问题