gpt4 book ai didi

java - 如何在java中按类型将文本文件中的信息排序到数组中?

转载 作者:行者123 更新时间:2023-12-01 14:07:40 25 4
gpt4 key购买 nike

我正在尝试编写一个方法来读取如下所示的文本文件:

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

起始字母是不同类型的订单。每种类型的订单都有不同的参数。我希望该方法以如下格式从文本文件中读取订单:输入价格地点 [税率] [关税]。我的困惑点是如何按类型将数据排序到数组中。

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)

最佳答案

嗯。这是一个编译错误。 Order 类不包含您正在调用的方法。例如。

    printOrder("Long")

检查类(class)顺序。这里你还没有提供

关于java - 如何在java中按类型将文本文件中的信息排序到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18769200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com