Reputation: 23
I am currently working on a program for one of my java class and I keep running into a wall with file reading! I am using a JTable to display the information so when the information is read from a file it is added to a row. Whenever there is a blank row the scanner cannot read it and throw an error! i get a java.util.NoSuchElementException on the last scanning line! As of right no i am suing two separate scanners. I had attempted to use the String.split method that that also gave me an error (ArrayIndexOutOfBoundsException: 0). I will post the reading a saving methods below (both two scanner version and split).
private void read() {
try {
Scanner scanner = new Scanner(new File("games.dat"));
scanner.useDelimiter(System.getProperty("line.separator"));
while (scanner.hasNext()) {
String[] tableRow = new String[6];
Scanner recIn = new Scanner(record);
recIn.useDelimiter("\\s*\\|\\s*");
tableRow[0] = recIn.next();
tableRow[1] = recIn.next();
tableRow[2] = recIn.next();
tableRow[3] = recIn.next();
tableRow[4] = recIn.next();
//recIn.next();
recIn.close();
model.addRow(new Object[]{tableRow[0],
tableRow[1], tableRow[2],
tableRow[3], tableRow[4]});
}scanner.close(); scanner = null;
} catch (Exception ex) {
JOptionPane.showConfirmDialog(null, "Could not connect to file! Make sure you are not in zipped file!",
"Warning!", JOptionPane.OK_OPTION,
JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}
private void save() {
for (int i = 0; i < model.getRowCount(); i++) {
String data = model.getValueAt(i, 0) + "|" + model.getValueAt(i, 1)
+ "|" + model.getValueAt(i, 2) + "|" + model.getValueAt(i, 3)
+ "|" + model.getValueAt(i, 4) + "|";
games.add(data);
}
try {
for (int i = 0; i < games.size(); i++) {
fileOut.println(games.get(i));
}
fileOut.close();
} catch (Exception ex) {
JOptionPane.showConfirmDialog(null, "Could not connect to file! Make sure you are not in zipped file!",
"Warning!", JOptionPane.OK_OPTION,
JOptionPane.ERROR_MESSAGE);
}
}
Upvotes: 2
Views: 1976
Reputation: 254
From the java.util.Scanner JavaDoc, there is this method, skip():
The last line reads, "Note that it is possible to skip something without risking a NoSuchElementException by using a pattern that can match nothing, e.g., sc.skip("[ \t]*")."
So, perhaps add as the first call of your loop, scanner.skip("[ \t]*);
Upvotes: 1
Reputation: 28762
recIn.next();
will fail if the line is empty, guard it with hasNext()
:
Scanner recIn = new Scanner(record);
recIn.useDelimiter("\\s*\\|\\s*");
if (recIn.hasNext()) {
tableRow[0] = recIn.next();
tableRow[1] = recIn.next();
tableRow[2] = recIn.next();
tableRow[3] = recIn.next();
tableRow[4] = recIn.next();
}
This assumes that when there is one element on the record, all of them are there. If this cannot be guaranteed, you need to protect each next()
call with hasNext()
and decide what to do when you run out of elements in the middle of a record.
Also, you seem to have an infinite loop:
while (scanner.hasNext()) {
// no calls to scanner.next()
}
Did you leave out String record = scanner.next();
from the top of that loop?
Upvotes: 2