gpt4 book ai didi

java - 文件扫描仪扫描仪未正确扫描所有信息

转载 作者:行者123 更新时间:2023-12-01 12:08:24 24 4
gpt4 key购买 nike

我有一个文件扫描器类,即用于从客户列表中读取名称(我还有另一个文件,即用于读取 Customer 对象,但这些文件正在被读得好)。我的列表如下所示(我只是跳过数字,然后将名称读入字符串name):

1、鲍比

2,乔

3、苏

4,玛丽

5、维克多

但由于某种原因,当我使用 toString() 打印时,显示的唯一名称是 Victor。我希望每个 customer 对象都有其自己的名称各自的名字。

问题:如何让 lineScanner 正确读取所有五个名称,然后将它们显示在 toString() 中?

//reads in customer name info
File customerList = new File("./src/Customers.txt");
Scanner fileScanner = new Scanner(customerList);

//while there is a new line, goes to next one
while(fileScanner.hasNextLine())
{
String line = fileScanner.nextLine();
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter(",");

//while there is a new attribute to read in on a given line, reads data
while(lineScanner.hasNext())
{
lineScanner.next();
name = lineScanner.next();
customer1 = new Customer(ranking, name, tvTimeTotal1, exerciseTimeTotal1);
customer2 = new Customer(ranking, name, tvTimeTotal2, exerciseTimeTotal2);
customer3 = new Customer(ranking, name, tvTimeTotal3, exerciseTimeTotal3);
customer4 = new Customer(ranking, name, tvTimeTotal4, exerciseTimeTotal4);
customer5 = new Customer(ranking, name, tvTimeTotal5, exerciseTimeTotal5);
}
}


System.out.println(customer1.toString());
System.out.println(customer2.toString());
System.out.println(customer3.toString());
System.out.println(customer4.toString());
System.out.println(customer5.toString());

最佳答案

类似这样的吗?

File customerList = new File("./src/Customers.txt");
Scanner fileScanner = new Scanner(customerList);

List<String> customerNames = new ArrayList<String>();
while(fileScanner.hasNextLine())
{
String line = fileScanner.nextLine();
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter(",");
lineScanner.next(); // Discard number
customerNames.add(lineScanner.next());
}

for(String name : customerNames)
{
System.out.println(name);
}

关于java - 文件扫描仪扫描仪未正确扫描所有信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27407806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com