Java input mismatch error using scanner

前端 未结 3 1503
时光取名叫无心
时光取名叫无心 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条回答
  •  猫巷女王i
    2021-01-26 19:53

    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.

提交回复
热议问题