该程序有 2 个类和一个 main 方法,应该从文件中读取。我的问题是该行double listPrice = fileScan.nextDouble();
给出了这样的错误异常Exception in thread "main" java.util.InputMismatchException
。错误在这个类中:
这是文件的内容:
本田雅阁
2004 16780.00 10.0
克莱斯勒 SUV
2002 8600.00 0.0
丰田凯美瑞
2007 21799.99 3.0
福特护航
2006 12345.78 5.5
//注意每行之间没有空格
class Proj1P2CarAryListService
{
private ArrayList<Proj1CarData> carList = new ArrayList<Proj1CarData>();
public Proj1P2CarAryListService()
{
carList = new ArrayList<Proj1CarData>();
}
public void readStoreCarsData()
{
Scanner scan = new Scanner(System.in);
Scanner fileScan;
boolean validName = false;
double discountAmount;
double netPrice;
do
{
System.out.print("Enter file name: ");
String str1 = scan.nextLine();
try
{
fileScan = new Scanner(new File(str1));
validName = true;
while (fileScan.hasNext())
{
String name = fileScan.nextLine();
String modelYear = fileScan.next();
double listPrice = fileScan.nextDouble();
double percentDiscount = fileScan.nextDouble();
discountAmount = listPrice * percentDiscount/100.0;
netPrice = listPrice - discountAmount;
Proj1CarData proj1 = new Proj1CarData(name, modelYear, listPrice, percentDiscount, discountAmount, netPrice);
carList.add(proj1);
System.out.println(proj1.toString());
}// end while
}// end try
catch (FileNotFoundException fnfe)
{
System.out.println("Invalid File name; enter again");
}
} while (!validName);
}//readStoreCarsData