-2

I`m MAC user and I have been struggling with this error. I search this error, now I just can't find solutions.

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 GradeBook.inputGrades(GradeBook.java:53)
at GradeBookTest.main(GradeBookTest.java:12)

It is Not a problem of the variable, and the code written As recorded in the book .

Touchstone
  • 5,575
  • 7
  • 41
  • 48
백지희
  • 1
  • 1
  • 1
  • 1
  • Can you provide the code which throws the exception? – TNT Mar 28 '15 at 04:06
  • 2
    The code has a call to `nextInt`, which means it's expecting a number. You're probably entering something that isn't a number. – Dawood ibn Kareem Mar 28 '15 at 04:11
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve) – Jim Garrison Mar 28 '15 at 04:14
  • 'while (input.hasNext()) { grade = input.nextInt(); total += grade; ++gradeCounter;' – 백지희 Mar 28 '15 at 04:43
  • probably this code have problem.. – 백지희 Mar 28 '15 at 04:43
  • `Introduction to Java Programming!! Enter the integer grades in the range 0-100 Type the end-of-file indicator to terminate input: On UNIX/Linux/Mac OS X type d then press Enter On Windows type z then press Enter 90 ^Z 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 GradeBook.inputGrades(GradeBook.java:52) at GradeBookTest.main(GradeBookTest.java:12)` <-console – 백지희 Mar 28 '15 at 04:48

2 Answers2

0

Check the following thing and also check whether you are respecting the rules:

public int nextInt()

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

For more details about next method visit:

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Touchstone
  • 5,575
  • 7
  • 41
  • 48
0

If you are using Switch case in the above program then give a space between switch and condition.

Dont write like this --> switch(n) Write like this --> switch (n)