0

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

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);


}
4

2 回答 2

0

您需要在for循环中进行以下更改。

for (int i = 0; i < orders.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) {
    }

    orders[i] = new Order(type, price, location, taxRate, tariff);
}
于 2013-09-12T05:14:11.620 回答
0
public static ArrayList<Order> readOrders (String fileName)
    {
        File file = new File (fileName);
        ArrayList<Order> o = new ArrayList<Order>();

        scan = null;
        try {
            scan = new Scanner(file);
        } catch (FileNotFoundException e) {
            System.out.println("Error, file not found: " + file.toString());
            e.printStackTrace();
        }


        while (scan.hasNext())
        {
            String fl = scan.next();

            if (fl.equals("N"))
            {
                NonProfitOrder n = new NonProfitOrder(scan.nextDouble(), scan.next());
                o.add(n);
            }
            else if (fl.equals("R"))
            {
                RegularOrder r = new RegularOrder (scan.nextDouble(), scan.next(), scan.nextDouble());
                o.add(r);
            }
            else
            {
                OverseasOrder oo = new OverseasOrder(scan.nextDouble(), scan.next(), scan.nextDouble(), scan.nextDouble());
                o.add(oo);
            }

        }
        return o;
    }
于 2013-09-19T00:46:37.313 回答