次のように整理されたデータを含むファイルがあります。
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.");
}