我正在尝试编写一个方法来读取如下所示的文本文件:
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 ArrayList<Order> 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[] order = new Order[8];
for (int i = 0; i < order.length; i++) {
String data = scan.nextLine(); // you need to use nextLine to read a whole line
String[] val = data.split(" ");
String type = val[0]; // Since its a String
double price = Double.parseDouble(val[1]);
String location = val[2]; // Since its a String
double taxRate = 0.0; // Default values
double tariff = 0.0; // Default values
try { // Incase they are not present - error handling
taxRate = Double.parseDouble(val[3]);
tariff = Double.parseDouble(val[4]);
} catch (ArrayIndexOutOfBoundsException e) {
}
ArrayList <Order> orders =new ArrayList<Order>(Arrays.asList(order));
return orders;
我无法让它与我的主要方法一起使用:
public static void main(String[] args)
{
ArrayList<Order> orders2 = readOrders("orders.txt");
for( Order o2 : orders2)
{
System.out.println( o2.printOrder("Long"));
}
for( Order o2 : orders2)
{
System.out.println(o2.printOrder("Short"));
}
}
这是我的错误代码:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method printOrder(String) is undefined for the type Order
The method printOrder(String) is undefined for the type Order
at prob1.OrderTester.main(OrderTester.java:19)