我经常在我遇到困难时使用此网站,但目前我找不到解决问题的方法,所以我会尝试提问。对不起,如果它可以很容易解决,但我现在已经尝试了几个小时,无法继续。
我收到了一个包含以下列表的文本文件:
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
现在,我必须将每个特定值存储到它自己的arraylist中。这很容易,只需使用保存逗号分割数据的变量来执行增强的forloop。每行都是它自己的预订。评论是可选的,所以并非所有评论都有。这使得使用分割是一种痛苦,因为我必须手动检查,如果没有,我在arraylist中添加一个空字符串值。
目前我的想法(可能不好)是使用一个计数到7的变量,然后重置以在每个arraylist中添加它的特定值。但是当我到达第一行中的最后一个值时,我遇到了问题。出于某种原因,当达到数字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)
答案 0 :(得分:1)
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]);
}
}
}
}
}
&#13;
答案 1 :(得分:0)
试试这个。
if(counter == 6)
quantities.add(的Integer.parseInt(element.trim()));
答案 2 :(得分:0)
public void loadBookings() {
String bookings = FileIO.readTextFile("bookings.txt");
//GregorianCalendar cal1;
//SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
int counter = 0;
String[] bookList = bookings.split(",");
for (String element : bookList) {
counter++;
System.out.println("Element to be persed:" + element);
if (counter == 1) {
tableNumbers.add(Integer.parseInt(element.trim()));
} else if (counter == 2) {
names.add(element);
} else if (counter == 3) {
surnames.add(element);
} else if (counter == 4) {
contacts.add(element);
} else if (counter == 5) {
dates.add(element);
} else if (counter == 6 || counter == 7) {
if (element.contains("\n")) {
String thisLineLastElement = "";
String nextLineFirstElement = "";
if (element.endsWith("\n")) {
thisLineLastElement = element.trim();
} else {
String[] split = element.split("\n");
thisLineLastElement = split[0];
nextLineFirstElement = split[1];
}
if (counter == 6) {
quantities.add(Integer.parseInt(thisLineLastElement.trim()));
comments.add("N/a");
} else if (counter == 7) {
comments.add(thisLineLastElement);
}
if (!element.endsWith("\n")) {
tableNumbers.add(Integer.parseInt(nextLineFirstElement.trim()));
}
counter = 1;
}
if (counter == 6) {
quantities.add(Integer.parseInt(element.trim()));
}
}
}
}
把这个拿出来。