I've been reading Java for the 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
I. M. D'Arthur
.001
Hugh R. DaReader
.212
It is taken right from the book's website so I think that there must be no error.
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
,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
I. M. D'Arthur
,001
Hugh 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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With