我有一个文件,其中的数据组织如下:
Hyundai Santa Fe
2005 16999 8
Mercury Mountaineer AWD
2004 17999 7.5
Mercury Grand Marquis
2006 19999 12.5
第一行是汽车名称,下一行是年份、价格和折扣金额。我正在尝试读取包含许多这些行的文件,并且令人惊讶的是价格和折扣并不总是表示为双倍。
这是我编写的一段代码,但没有正确解析这些代码。我收到输入不匹配异常。
我究竟做错了什么?
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.");
}