I have a file with data organized like so:
Hyundai Santa Fe
2005 16999 8
Mercury Mountaineer AWD
2004 17999 7.5
Mercury Grand Marquis
2006 19999 12.5
The first line being car name, next line being year, price, and discount amount. I'm trying to read a file with many of these lines and the kicker is that price and discount isn't always represented as a double.
Here is a snippet of code which I wrote but does not properly parse through the lines. I get an input mismatch exception.
What am I doing wrong?
try {
Scanner scanFile = new Scanner(file);
while(scanFile.hasNext()) {
String carName = scanFile.nextLine();
int year = scanFile.nextInt();
double listPrice = scanFile.nextDouble();
double percentDiscount = scanFile.nextDouble();
double discountAmount = calculateDiscount(listPrice, percentDiscount);
double netPrice = calculateNetPrice(listPrice, discountAmount);
carList.add(new Proj1CarData(carName, year, listPrice, percentDiscount, discountAmount, netPrice));
}
} catch(FileNotFoundException fnfe) {
System.out.println("Unable to locate the file supplied.");
}