我有这三种方法,其中read()用于从' database.db'中读取存储的数据。形式:名字|姓氏| mi | dob | last met |特殊工作|注释。 (注意:替换' |'用'`')iterations()方法用于查找&data;数据库中的行数。为了知道read()中for循环的循环次数。最后是有问题的方法,createUIComponents()方法。该方法从read()接收String [] [],从iterations()接收迭代以在循环中使用并将它们存储在列表模型中。然而,一旦数据库中有数据,这些方法就可以很好地工作。经过第8行或第8次迭代,它不会将其存储在列表模型中。有人可以详细说明我做错了什么吗? 公共类读者{
public static String[][] read() throws IOException {
File database = new File("database.db");
if(database.exists()) {
int iterations = iterations();
BufferedReader in = new BufferedReader(new FileReader("database.db"));
String[][] dbalt = new String[iterations][8];
System.out.println("Iterations: " + iterations);
for (int i = 0; i != iterations; i++) {
dbalt[i] = in.readLine().split("`");
System.out.println(Arrays.toString(dbalt[i]));
}
return dbalt;
}
return null;
}
public static int iterations() throws IOException {
int iterations = 0;
BufferedReader in = new BufferedReader(new FileReader("database.db"));
while (true) {
final String line = in.readLine();
if (line == null) break;
iterations++;
}
in.close();
return iterations;
}
}
//这是一个单独的类
private void createUIComponents() {
DefaultListModel<String> listmodel = new DefaultListModel<>();
try {
String[][] store = Reader.read();
for (int i = 0; i != Reader.iterations(); i++) {
try {
if (!(store[i][i].equals(""))) {
listmodel.addElement(Arrays.toString(store[i]));
System.out.println("Listmode=" + Arrays.toString(store[i]));
}
} catch (ArrayIndexOutOfBoundsException e) {}
}
} catch(IOException e) {}
list1 = new JList<>(listmodel);
JScrollPane addedtolist = new JScrollPane(list1);
}
对于调试,以下是使用&#39; database.db&#39;输出sysouts的示例。含: 蓬松|猫| A | 6/9/2018 | 5/7/2018 |非常蓬松|()4606 763-0932 |棚子太多1 蓬松|猫| A | 6/9/2018 | 5/7/2018 |非常蓬松|()4606 763-0932 |棚子太多2 蓬松|猫| A | 6/9/2018 | 5/7/2018 |非常蓬松| (460)763-0932 |棚子太多3 蓬松|猫| A | 6/9/2018 | 5/7/2018 |非常蓬松|()4606 763-0932 |棚子太多4 蓬松|猫| A | 6/9/2018 | 5/7/2018 |非常蓬松|()4606 763-0932 |棚子太多5 蓬松|猫| A | 6/9/2018 | 5/7/2018 |非常蓬松| (460)763-0932 |棚子太多6 蓬松|猫| A | 6/9/2018 | 5/7/2018 |非常蓬松| (460)763-0932 |棚子太多7 蓬松|猫| A | 6/9/2018 | 5/7/2018 |非常蓬松|()4606 763-0932 |棚子太多8 蓬松|猫| A | 6/9/2018 | 5/7/2018 |非常蓬松|()4606 763-0932 |棚子太多9 蓬松|猫| A | 6/9/2018 | 5/7/2018 |非常蓬松| (460)763-0932 |棚子太多10
注意:替换&#39; |&#39;与&#39;`&#39;。
输出:
Iterations: 10
[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much1]
[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much2]
[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much3]
[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much4]
[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much5]
[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much6]
[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much7]
[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much8]
[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much9]
[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much10]
Listmode=[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much1]
Listmode=[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much2]
Listmode=[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much3]
Listmode=[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much4]
Listmode=[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much5]
Listmode=[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much6]
Listmode=[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much7]
Listmode=[Fluffy, Cat, A, 6/9/2018, 5/7/2018, Is very fluffy, (460)763-0932, Sheds too much8]