gpt4 book ai didi

java - 拆分文本文件条目以添加到数组列表中

转载 作者:行者123 更新时间:2023-12-02 11:50:03 25 4
gpt4 key购买 nike

我通常在遇到困难时使用此网站,但目前我无法找到问题的解决方案,因此我会尝试提出问题。抱歉,如果解决起来可能很容易,但我已经尝试了几个小时,但无法继续。

我得到了一个文本文件,其中包含以下列表:

1,John,Spiteri,99845124,10/05/2018,2
2,Kate,Galea,79451245,10/05/2018,4,Highchair required
1,Peter,Zahra,79845731,21/07/2018,3
3,Alice,Carabott,99468791,10/05/2018,10
4,James,Dingli,99364125,10/05/2018,15,Birthday
2,Kim,Fenech,99871632,21/07/2018,4,Cake Ordered
5,George,Pace,79541247,10/05/2018,2,Vegetarian Menu
3,Jack,Vella,99451785,21/07/2018,2

现在,我必须将每个特定值存储到它自己的数组列表中。这很简单,只需使用保存由逗号分隔的数据的变量来执行增强的 for 循环即可。每条线路都是它自己的预订。评论是可选的,因此并非所有人都有评论。这使得使用 split 变得很痛苦,因为我必须手动检查,如果没有任何内容,我会在数组列表中添加一个空字符串值。

目前我的想法(可能不好)是使用一个计数到 7 的变量,然后重置以在每个数组列表中添加它的特定值。但是,当我到达第一行的最后一个值时,我遇到了问题。由于某种原因,当达到数字 2 时,即使它们是分开显示的,我仍然收到错误 NumberFormatException。我的 if 语句检查是否有评论也不起作用。

我将在下面发布方法。如果有人可以伸出援手,我将不胜感激:)我不介意只是解释,我来这里是为了学习不作弊。谢谢。

ArrayList<Integer> tableNumbers = new ArrayList<>();
ArrayList<String> names = new ArrayList<>();
ArrayList<String> surnames = new ArrayList<>();
ArrayList<String> contacts = new ArrayList<>();
ArrayList<String> dates = new ArrayList<>(); // To be changed!!!
ArrayList<Integer> quantities = new ArrayList<>();
ArrayList<String> comments = new ArrayList<>();

int counter = 0;
String[] bookList = bookings.split(",");

for(String element: bookList){
counter++;
System.out.println(element);

if(counter == 1)
tableNumbers.add(Integer.parseInt(element));
if(counter == 2)
names.add(element);
if(counter == 3)
surnames.add(element);
if(counter == 4)
contacts.add(element);
if(counter == 5)
dates.add(element);
if(counter == 6)
quantities.add(Integer.parseInt(element)); // Line 86
if(counter == 7){
char comment = element.charAt(0);

if(element.length() < 2 && Character.isDigit(comment)){
comments.add(" ");
tableNumbers.add(Integer.parseInt(element));
counter = 1;
}
else {
comments.add(element);
counter = 0;
}
}
}

错误消息:

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "2 
2"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at bistronomic.restaurant.Main.loadBookings(Main.java:86)
at bistronomic.restaurant.Main.<init>(Main.java:108)
at bistronomic.restaurant.Main$7.run(Main.java:387)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

最佳答案

试试这个,我只是改变你循环的方式,使其更容易访问,你可以更改文件访问字符串[],如果我正确理解问题,这应该可以解决问题

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Demo {

public static void main(String[] args) throws IOException {

ArrayList<Integer> tableNumbers = new ArrayList<>();
ArrayList<String> names = new ArrayList<>();
ArrayList<String> surnames = new ArrayList<>();
ArrayList<String> contacts = new ArrayList<>();
ArrayList<String> dates = new ArrayList<>(); // To be changed!!!
ArrayList<Integer> quantities = new ArrayList<>();
ArrayList<String> comments = new ArrayList<>();

try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
String line;
while ((line = br.readLine()) != null) { // till here each line alone

String dataEachLine[] = line.split(",");

tableNumbers.add(Integer.parseInt(dataEachLine[0]));
names.add(dataEachLine[1]);
surnames.add(dataEachLine[2]);
contacts.add(dataEachLine[3]);
dates.add(dataEachLine[4]);
quantities.add(Integer.parseInt(dataEachLine[5]));
if (dataEachLine.length > 6) {
comments.add(dataEachLine[6]);

}
}
}
}


}

关于java - 拆分文本文件条目以添加到数组列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47954072/

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