我正在尝试编写一个方法来读取如下所示的文本文件:
N 1000.0 NY
R 2000.0 CA 0.09
R 500.0 GA 0.07
N 2000.0 WY
O 3000.0 Japan 0.11 20.0
N 555.50 CA
O 3300.0 Ecuador 0.03 30.0
R 600.0 NC 0.06
起始字母是不同类型的订单。每种类型的订单都有不同的参数。我希望该方法以如下格式从文本文件中读取订单:键入价格位置 [TaxRate] [关税]。我的困惑是如何将字符串数据添加到数组中。
public static void readOrders (String fileName)
{
File file = new File (fileName);
scan = null;
try {
scan = new Scanner(file);
} catch (FileNotFoundException e) {
System.out.println("Error, file not found: " + file.toString());
e.printStackTrace();
}
Order[] orders = new Order[8];
for (int i = 0; i < orders.length; i++)
{
String data = scan.next();
String [] val = data.split(" ");
// String type = ?? (val[0]);
double price = Double.parseDouble(val[1]);
// String location = ?? (val[2]);
double taxRate = Double.parseDouble(val[3]);
double tariff = Double.parseDouble(val[4]);
Order o = new Order (type, price, location, taxRate, tariff);
orders[i] = o;
}
scan.close();
System.out.println("All Orders");
for (Order o : orders)
System.out.println(o);
}