My scanner is reading from a text file with the use of a delimiter. However when I run the program the only line that gets printed out is the first line of data then I get thrown an input mismatch exception. I believe the error is that it doesn't move onto the next line. I understand how the mismatch works I am just unsure how to fix it. I have tried putting scanner.nextLine(); in as you can see in my code below.
Here is my code for the scanner :
/**
* Method for reading the data from the electricToolData.txt file.
*/
public void readElectricToolData()
{
Frame myFrame = null; // initialises frame to null
FileDialog fileBox = new FileDialog(myFrame,
"Open", FileDialog.LOAD);
fileBox.setVisible(true);
String directoryPath = fileBox.getDirectory();
String filename = fileBox.getFile();
System.out.println(filename + " " + directoryPath); // prints out name of file and directory path
try {
File dataFile = new File(filename);
Scanner scanner = new Scanner(dataFile);
while(scanner.hasNext())
{
String lineOfText = scanner.nextLine(); // reads the next line of the file and stores as String
//if statement checks if line starts with either "//" or space.
if (lineOfText.startsWith("//") || lineOfText.isEmpty())
{
}
else // now got real data
{
Scanner scanner2 = new Scanner(lineOfText);
scanner2.useDelimiter(",");
lineOfText.trim();
System.out.println(lineOfText);
while(scanner2.hasNext())
{
//lineOfText.trim();
boolean mRechargeable = scanner.nextBoolean();
String mPower = scanner.next();
String mToolName = scanner.next();
String mItemCode = scanner.next();
int mTimesBorrowed = scanner.nextInt();
boolean mOnLoan = scanner.nextBoolean();
int mCost = scanner.nextInt();
int mWeight = scanner.nextInt();
scanner.nextLine();
ElectricTool electricTool = new ElectricTool(mToolName, mItemCode, mTimesBorrowed, mCost, mWeight, mPower);
toolsList.add(electricTool);
}
}
//System.out.println(lineOfText); // prints out string
}
scanner.close();
scanner.close(); // closes scanner
}
catch(FileNotFoundException e)
{
System.err.println("Caught IOException: " + e.getMessage());
}
}
The error gets shown at the boolean mRechargeable = scanner.nextBoolean();.
Here is the data file :
// this is a comment, any lines that start with //
// (and blank lines) should be ignored
// data is rechargeable, power, toolName, itemCode, timesBorrowed, onLoan, cost, weight
true,18V,Makita BHP452RFWX,RD2001,12,false,14995,1800
true,10.8V,Flex Impact Screwdriver FIS439,RD2834,14,true,13499,1200
false,1350W,DeWalt D23650-GB Circular Saw, RD6582,54,true,14997,5400
false,1500W,Milwaukee DD2-160XE Diamond Core Drill,RD4734,50,false,38894,9000
true,10.8V,Bosch GSR10.8-Li Drill Driver,RD3021,25,true,9995,820
false,900W,Bosch GSB19-2REA Percussion Drill,RD8654,85,false,19999,4567
true,10.8V,Flex Impact Screwdriver FIS439, RD2835,14,false,13499,1200
true,18V,DeWalt DW936 Circular Saw,RD4352,18,false,19999,3300
false,2100W,Sparky FK652 Wall Chaser,RD7625,15,false,29994,8400
The problem is that String.trim() doesn't work the way you think it does: it doesn't mutate the String it is called on, but returns a new String that effectively has the whitespace removed. This is causing the line to not be trimmed, and that "blank line" that has a lot of space characters on it then fails to be parsed with your first scanner.nextBoolean() call.
So, update:
String lineOfText = scanner.nextLine(); // reads the next line of the file and stores as String
to be:
// read the next line of the file, removing enclosing whitespace
String lineOfText = scanner.nextLine().trim();
Comments should ideally preceed the line, as it is easier to read and format, and more obvious that it is a comment. Also remove the redundant lineofText.trim(); later in the code.
Further, remember to close all Scanner instances when finished (so here one for each line, and one for the file).
The next problem is that in the inner loop, where you construct your ElectricTool instances, you are calling methods on scanner, rather than scanner2 (rename this to something more semantic, eg itemScanner):
Scanner itemScanner = new Scanner(lineOfText);
itemScanner.useDelimiter(",");
boolean mRechargeable = itemScanner.nextBoolean();
String mPower = itemScanner.next();
String mToolName = itemScanner.next();
String mItemCode = itemScanner.next();
int mTimesBorrowed = itemScanner.nextInt();
boolean mOnLoan = itemScanner.nextBoolean();
int mCost = itemScanner.nextInt();
int mWeight = itemScanner.nextInt();