File customer = new File("Cus.txt");
Scanner readCustomer = new Scanner(customer);
while(readCustomer.hasNextLine())
{
String line = readCustomer.nextLine();
String delims = ", ";
String[] split = line.split(delims);
int arr = Integer.parseInt(split[0]);
int ser = Integer.parseInt(split[1]);
int qui = Integer.parseInt(split[2]);
int appt = Integer.parseInt(split[3]);
int appL = Integer.parseInt(split[4]);
Customer newCustomer = new Customer(arr, ser, qui, appt, appL);
customerList.add(newCustomer);
System.out.println("Customer arrival: " + newCustomer);
}readCustomer.close();
输出
912, 4, 922, 0, 0
915, 5, -1, 10, 10
918, 0, -1, 5, 5
920, 0, -1, 10, 10
925, 6, 930, 0, 0
CUS.TXT文件
915, 5, -1, 925, 10,
918, 0, -1, 920, 5,
920, 0, -1, 915, 10,
925, 6, 930, -1, 0,
我真的很茫然,不知道如何解决这个问题。有没有人看到任何错误或为什么它在我的分裂中无法读取[4]?为什么要复制int appt值中的内容?
答案 0 :(得分:1)
@ Jamie我无法理解你是如何获得输出的,因为你打印的是对象而不是值。我在你的代码中没有改变任何东西,它对我来说很好。你可能错过了很小的东西。使用以下代码,您将能够获得所需的输出。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class ReadingInputIncorrectly {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<Customer> customerList=new ArrayList<>();
File customer = new File("path to your Cus.txt file");
Scanner readCustomer = new Scanner(customer);
while (readCustomer.hasNextLine()) {
String line = readCustomer.nextLine();
String delims = ", ";
String[] split = line.split(delims);
int arr = Integer.parseInt(split[0]);
int ser = Integer.parseInt(split[1]);
int qui = Integer.parseInt(split[2]);
int appt = Integer.parseInt(split[3]);
int appL = Integer.parseInt(split[4]);
Customer newCustomer = new Customer(arr, ser, qui, appt, appL);
customerList.add(newCustomer);
System.out.println(newCustomer.num1+", "+newCustomer.num2+", "+newCustomer.num3+", "+newCustomer.num4+", "+newCustomer.num5+", ");
}
readCustomer.close();
}
}
class Customer{
int num1,num2,num3,num4,num5;
Customer(int num1,int num2,int num3,int num4,int num5){
this.num1=num1;
this.num2=num2;
this.num3=num3;
this.num4=num4;
this.num5=num5;
}
}
<强>输出强>
915, 5, -1, 925, 10,
918, 0, -1, 920, 5,
920, 0, -1, 915, 10,
925, 6, 930, -1, 0,
Cus.txt文件
915, 5, -1, 925, 10,
918, 0, -1, 920, 5,
920, 0, -1, 915, 10,
925, 6, 930, -1, 0,
让我知道它是否有效。