私が抱えているこのエラーのために他のものが動作しているかどうかはわかりません。私はtxtファイル "customerOrders.txt"から読んで、顧客あたりの製品注文を数えようとしています。表示方法は完了していませんが、書式設定は問題ではありません。このjava.lang.NumberFormatErrorをどうやって渡すのですか?私は何かを逃していますか?アレイファイルIOプロジェクトはコンパイルされますが、実行されません。 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秒 customerOrders.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());行でエラーが発生しています。 希望すること –
エラーを生成する入力データを表示します。 –