The exception on the main line is java.util.InputMismatchException

I read Java for dummies and I came across this error:

Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextDouble(Unknown Source) at TeamFrame.<init>(TeamFrame.java:18) at ShowTeamFrame.main(ShowTeamFrame.java:7) 

This is the code:

 import java.text.DecimalFormat; public class Player { private String name; private double average; public Player(String name, double average) { this.name=name; this.average=average; } public String getName() { return name; } public double getAverage() { return average; } public String getAverageString() { DecimalFormat decFormat = new DecimalFormat(); decFormat.setMaximumIntegerDigits(0); decFormat.setMaximumFractionDigits(3); decFormat.setMinimumFractionDigits(3); return decFormat.format(average); } } import java.util.Scanner; import java.io.File; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.GridLayout; @SuppressWarnings("serial") public class TeamFrame extends JFrame { public TeamFrame() throws IOException { Player player; Scanner hankeesData = new Scanner(new File("d:/eclipse/Workplace/10-02 book/Hankees.txt")); for (int num = 1; num <= 9; num++) { player = new Player(hankeesData.nextLine(), hankeesData.nextDouble()); hankeesData.nextLine(); addPlayerInfo(player); } setTitle("The Hankees"); setLayout(new GridLayout(9, 2, 20, 3)); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setVisible(true); hankeesData.close(); } void addPlayerInfo(Player player) { add(new JLabel(" " + player.getName())); add(new JLabel(player.getAverageString())); } } import java.io.IOException; class ShowTeamFrame { public static void main(String args[]) throws IOException { new TeamFrame(); } } 

This is the .txt file:

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

This is taken directly from the book's website, so I think there should be no mistake.

+5
source share
3 answers

Paste this into your code after creating hankeesData:

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

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

Update: If you get NoSuchElementException: No line found in the last line of input, this is because your last line does not have \ n. Use hasNextLine() as shown near the end of the HyperZ response.

+4
source

The text file should be formatted as follows:

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

So the problem was that a double was represented as x.xxx instead of x,xxx , which caused an exception!

This solves the current exception, but with the same code you get one more error: Exception in thread "main" java.util.NoSuchElementException: No line found

Take a 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 repeating the last time (i.e. num = 9 ) we read the name of the player, then we read it on average. But then again we are hankeesData.nextLine(); ! However, the mean was the last line in this text file, and there is nothing more to read, so doing hankeesData.nextLine(); one last time will result in a 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); } 
+1
source

An exception is an InputMismatchException Scanner.nextDouble() method. Javadocs for Scanner says InputMismatchException

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

In the same documents, we can find the regular expression Float.

 Float: Decimal | HexFloat | SignedNonNumber Decimal: ( [-+]? 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 | LocalInfinity SignedNonNumber: ( [-+]? NonNumber ) | LocalPositivePrefix NonNumber LocalPositiveSuffix | LocalNegativePrefix NonNumber LocalNegativeSuffix 

For your case, the only important part is in decimal form, a regular expression for the binary base number.


To fix your code, try adding the following:

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

front:

  hankeesData.nextLine(); 
-2
source

All Articles