我正在尝试从此文本文件中获取信息:
Flinders Street
amy j
leanne j
chris s
1 normal 1 [(o, 21) (o, 17) (t, 3)]
2 underAge 2 [(t, 4) (i, 6)]
3 elderly 3 [(o, 12) (t, 5) (i, 7)] 3
4 normal 4 [(t, 4) (t, 3) (t, 8) (t, 2)]
5 underAge 5 [(o, 20) (i, 12)]
6 underAge 13 [(o, 20) (t, 5) (t, 3)]
7 elderly 25 [(t, 4) (t, 3) (i, 12)] 0
8 normal 27 [(t, 2) (t, 2)]
9 underAge 28 [(i, 2)]
我想把工作人员(amy,leanne和chris)放到一个arraylist以及下面的组中,这是另一个arraylist中与他们相关的客户和价值观的列表。我试图在下面做到这一点:
public static void readFile(String file) {
try {
//Using the buffered reader to load the file.
BufferedReader br = new BufferedReader(new FileReader("C:\\input\\" + file));
int listLocation = 0;
while (br.ready()) {
String next = br.readLine().trim();
if (next.isEmpty()) {
listLocation++;
}
if (listLocation == 0) {
Main.branch = next;
}else if (listLocation == 1) {
Main.staff.add(next);
}else if (listLocation == 2) {
Main.customers.add(next);
}
listLocation++;
}
} catch (Exception ex) { }
}
答案 0 :(得分:1)
在if-else here:
之后删除listLocation++;
if (next.isEmpty()) { listLocation++; } if (listLocation == 0) { Main.branch = next; }else if (listLocation == 1) { Main.staff.add(next); }else if (listLocation == 2) { Main.customers.add(next); } listLocation++; // ^^^ remove this line
实际情况就是这样:
listLocation
为0,设置为Main.branch
listLocation
增加到1 listLocation
增加到2 listLocation
为2,将行添加到Main.customers
(错误地,错误)listLocation
增加到3 listLocation
永远增加,listLocation
值的所有条件都不会再次匹配