gpt4 book ai didi

java - 数组数组到表

转载 作者:太空宇宙 更新时间:2023-11-04 13:50:31 26 4
gpt4 key购买 nike

面临将数组数组打印到表的问题。我的程序将表数据保存到单独的文本文件(每天都有单独的文件)。现在我想阅读全部内容并对每个字段求和。我保存的文本文件如下所示:

0   Name    auto    note    note.tot    insurance   ins.tot offer
1 john 51 6 2 21 44 12
2 peter 32 5 1 36 65 20
3 smith 12 2 1 45 53 9
4 mike 5 0 0 17 55 11

我已经找到了一种读取它并将数值保存到数组[][]的方法。但现在我需要将该数组打印到表中。

这是我的读取文件代码:

OK4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File f = new File(fileName);
if (f.exists()) {
try {
tableModel = new DefaultTableModel(new Object[] {"#", "Name", "auto", "note", "note.tot", "insurance", "ins.tot", "offer"},0);
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line = br.readLine();
String[] colHeaders = line.split("\\s+");
tableModel.setColumnIdentifiers(colHeaders);

while ((line = br.readLine()) != null) {
String[] data = line.split("\\s+");
tableModel.addRow(data);
}
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "This date was not saved");
};
table.setModel(tableModel);
table.getColumnModel().getColumn(0).setMaxWidth(22);
}
});

这是我遇到问题的代码:

OK5.addActionListener(new ActionListener() {
@Override

public void actionPerformed(ActionEvent e) {
int[][] array = null;
int counter = 0;
mon = (String) month.getValue();
for (int i = 1; i < 32; i++) {
counter = 0;
fileName= mon + i + ".txt";
File f = new File(fileName);
if (f.exists()) {
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line = br.readLine();
String[] colHeaders = line.split("\\s+");
tableModel = new DefaultTableModel(new Object[] {"#", "name", "auto", "note", "note.tot", "insurance", "ins.tot", "offer"}, 0);
tableModel.setColumnIdentifiers(colHeaders);

while ((line = br.readLine()) != null) {
counter = counter + 1;
String[] info = line.split("\\s+");
for(int j = 2; j < 8; j++) {
int num = Integer.parseInt(info[j]);
array[j][counter]=array[j][counter] + num;
}
};
} catch (Exception ex) {}
};
};

// and here is trying to display it. but it wont work
tableModel.addRow(array[1][]);
}
});

错误代码是:int 无法转换为 vector

但是我应该将其转换为 vector 吗?可能还有另一种方式在表格中显示它?

最佳答案

实际上错误代码是完全没有意义的,因为实际的错误只是矩阵的访问:tableModel.addRow(array[1][]);实际上应该导致第二个括号([])的语法错误。要修复语法错误,请将该行替换为 tableModel.addRow(array[1]);。一般注意事项:请格式化您的代码,它完全不可读。

关于java - 数组数组到表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30333333/

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