阵列文件IO项目编译但不会运行。获取java.lang.NumberFormatError解决方案?

时间:2016-07-16 04:58:12

标签: java arrays

不确定是否有其他因为这个错误导致我卡住了。我正在读取txt文件“customerOrders.txt”并尝试计算每个客户的产品订单。显示方法尚未完成,但格式不应该是问题。我如何通过这个java.lang.NumberFormatError传递?我只是错过了一些东西吗?

txt文件包括:

1,First1,Last1,3
101,Product1,1,2000.00
102,Product2,3,500.00
103,Product3,1,1000.00
2,First2,Last2,2
102,Product2,3,500.00
103,Product3,1,1000.00
3,First3,Last3,1
104,Product4,1,1500.00
4,First4,Last4,4
101,Product1,1,2000.00
102,Product2,3,500.00
103,Product3,1,1000.00
104,Product4,1,1500.00
5,First5,Last5,1
101,Product1,2,2000.00
行出现

错误: productCount = Integer.parseInt(fin.nextLine());

import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
/**
 * 
 */
public class Main
{
    // TODO: declare the ArrayList of Customer objects.
    private static ArrayList<Customer> customer = new ArrayList<>();



public static void main(String[] args) 
{
    // read the customer and order records from the file.
    populateCustomersFromFile("customerOrders.txt");

    // display total number of orders and total amount owed.
    displayOrderSummary();

    // display each customer and their orders
    displayCustomerOrders();

    // write the customer and order details to the file.
    writeCustomersToFile("customerOrdersOut.txt");
}

// TODO: implement the populateCustomersFromFile method.
public static void populateCustomersFromFile(String filename){
    try ( Scanner fin = new Scanner(new File(filename) ) ) {
        String record;
        String[] fields; 
        String[] productFields;
        ProductOrder[] productOrder;

        int productCount;
        while ( fin.hasNext() ) {
            record = fin.nextLine();
            fields = record.split(",");

            productCount = Integer.parseInt( fin.nextLine());
            productOrder = new ProductOrder[productCount];

            for ( int product = 1; product <= productCount; product++) {
                record = fin.nextLine();
                productFields = record.split(",");

                productOrder[product - 1] = new ProductOrder(productFields[0],
                                                             productFields[1],
                                                             Integer.parseInt(productFields[2]),
                                                             Double.parseDouble(productFields[3]));
            }

            customer.add( new Customer (fields[0],
                                       fields[1],
                                       fields[2],
                                       Integer.parseInt(fields[3]),
                                       productOrder)
                                       );



        }
    }catch (FileNotFoundException e) {
        System.err.println("Error: Unable to find file: ");
        System.exit(1);
    }
}

// TODO: implement the displayOrderSummary method. See screen cast in README file for output.
public static void displayOrderSummary(){
    System.out.println("Order Summary:");
    System.out.println("----------------");
    for (Customer c : customer){
        System.out.println("Orders: " + c);
    }
}

// TODO: implement the displayCustomerOrders method. See screen cast in README file for output.
public static void displayCustomerOrders(){
    System.out.println("Customer Orders:");
    System.out.println("----------------");

}

// TODO: implement the writeCustomersToFile method. See customerOrdersOut.txt for format.
public static void writeCustomersToFile(String filename) {
    try ( PrintWriter pw = new PrintWriter(filename) ) {
        for (Customer c : customer) {
            pw.println(c.getCid() + "," + 
                       c.getFirst() + "," + 
                       c.getLast() + "," + 
                       c.getNumberOfOrders());

            pw.println( c.getProductOrder().length );           

            for ( ProductOrder po : c.getProductOrder() ) {
                pw.println( po.getPid() + "," +
                            po.getDescription() + "," +
                            po.getQty() + "," +
                            po.getCost() );
            }
        }

    } catch (FileNotFoundException e) {
        System.err.println("Error - unable to write to file.");
        System.exit(1);
    }
}
}

1 个答案:

答案 0 :(得分:0)

在第

productCount = Integer.parseInt( fin.nextLine());

您试图将整行解析为整数,这显然不起作用。您刚才将该行拆分为字段。你应该解析

productCount = Integer.parseInt(fields[0]);