When I try to load the data from the textfile, with data like this
java;sdf;2.0;3.0;
cpp;sdks;24.6;89.0;
I'm getting the NoSuchElementException
with this code.
public void loadFile() {
Scanner scan = null;
int n = 0;
try {
scan = new Scanner(new File(this.filename)).useDelimiter(";");
this.items.clear();
while (scan.hasNextLine()) {
String name = scan.next();
String barcode = scan.next();
double unitPrice = scan.nextDouble();
double shipCost = scan.nextDouble();
Product newProduct = new Product(name, barcode, unitPrice, shipCost);
this.items.add(newProduct);
n++;
}
}
catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
} catch (InputMismatchException e) {
System.err.println("Caught InputMismatchException: " + e.getMessage());
System.out.println(n + " products loaded!");
} catch (NoSuchElementException e) {
System.err.println("Attempt to read past end of file");
} catch (IllegalStateException e) {
System.err.println("Attempt to read a closed file");
} finally {
if (scan != null)
scan.close();
}
}