exception in thread main - java.util.InputMismatchException exception in thread main - java.util.InputMismatchException multithreading multithreading

exception in thread main - java.util.InputMismatchException


Insert this in your code after hankeesData is created:

hankeesData.useLocale(java.util.Locale.ENGLISH);

I noticed that your program works if the "." is replaced with ",", and the new code now tells Scanner to use the english method.

Update:If you get a NoSuchElementException: No line found at the last input line, then this is because your last line doesn't have a \n. Use hasNextLine() as shown near the end of the answer of HyperZ.


The text file should be formatted like this :

Barry Burd,101Harriet Ritter,200Weelie J. Katz,030Harry "The Crazyman" Spoonswagler,124Filicia "Fishy" Katz,075Mia, Just "Mia",111Jeremy Flooflong Jones,102I. M. D'Arthur,001Hugh R. DaReader,212

So the problem was that the value of type double was provided like this x.xxx instead of x,xxx , which was causing the exception!

This solves the current exception, however with the code unchanged you will get another error : Exception in thread "main" java.util.NoSuchElementException: No line found

Let us look at the for loop :

for (int num = 1; num <= 9; num++) {    player = new Player(hankeesData.nextLine(),                hankeesData.nextDouble());    hankeesData.nextLine(); // Go to next line, this is causing the problem    addPlayerInfo(player);}

When iterating the last time (i.e. num = 9) we read the player's name, then we read it's average. But then we do hankeesData.nextLine(); again!However, the average value was the last line in this text file and there is nothing more to read, hence doing hankeesData.nextLine(); a last time will result in the No line found exception.

for (int num = 1; num <= 9; num++) {    player = new Player(hankeesData.nextLine(),                    hankeesData.nextDouble());    if (hankeesData.hasNextLine())       hankeesData.nextLine(); // Go to next line, only if there is a next line!    addPlayerInfo(player);}


The Exception you're is an InputMismatchExceptionthrown by the Scanner.nextDouble() method. In the Javadocs for Scanner, it is stated that InputMismatchException is thrown

if the next token does not match the Float regular expression, or is out of range

In the same docs we can find the Float regular expression.

Float:    Decimal | HexFloat | SignedNonNumberDecimal:    ( [-+]? DecimalNumeral Exponent? )    | LocalPositivePrefix DecimalNumeral LocalPositiveSuffix Exponent?    | LocalNegativePrefix DecimalNumeral LocalNegativeSuffix Exponent?HexFloat:    [-+]? 0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+ ([pP][-+]?[0-9]+)?NonNumber:   NaN | LocalNan | Infinity | LocalInfinitySignedNonNumber:    ( [-+]? NonNumber )    | LocalPositivePrefix NonNumber LocalPositiveSuffix    | LocalNegativePrefix NonNumber LocalNegativeSuffix

For your case the only important part is under decimal, the regex for a base 10 double number.


To fix your code, try adding this:

 if(!hankeesData.hasNextLine()){     break; }

before:

 hankeesData.nextLine();