阵列文件IO项目已编译但无法运行。获取java.lang.NumberFormatError解决方案?
由于这个错误,我不确定是否还有其他方法可以工作。我正在读取txt文件customerOrders.txt,并试图统计每个客户的产品订单。显示方法尚未完成,但格式不应成为问题。如何通过此java.lang.NumberFormatError?我是不是错过了什么 txt文件包括:阵列文件IO项目已编译但无法运行。获取java.lang.NumberFormatError解决方案?,java,arrays,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,Produ
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);
}
}
}
排队
productCount = Integer.parseInt( fin.nextLine());
您试图将整行解析为整数,这显然行不通。在此之前,您已将行拆分为字段。你应该在分析
productCount = Integer.parseInt(fields[0]);
如果不显示导致错误的输入数据,任何人都无法帮助您。oops 1秒customerOrders.txt是1,First1,Last1,3 101,Product1,12000.00 102,Product2,3500.00 103,Product3,11000.00 2,First2,Last2,2 102,Product2,3500.00 103,Product3,11000.00 3,Last3,1 104,Product4,11500.00 4,First4,Last4,4 101,Product1,12000.00 102,Product2,3500.00 103,Product3,11000.00 104,Product4,11500.00 5,First5,Last5,1 101,Product1,22000.00行productCount=Integer.parseInt fin.nextLine出现错误;希望这有帮助。显示产生错误的输入数据。不,你的帖子并将数据包含在那里,而不是在评论中。对不起,对该网站来说真的很新。有没有办法添加txt文件本身或只是把它放在文本中?谢谢!这解决了这个问题。你介意解决另一个问题吗?很抱歉,解析对我来说是新的。Integer.parseIntproductFields[2]现在出现错误,这在for循环中,是文本文件中的字符串Last2,应计为整数