将数组数组打印到表格面临的问题。我的程序将表数据保存为单独的文本文件(每天单独的文件)。现在我想要阅读所有内容并对每个字段求和。 我保存的文本文件如下所示:
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
但我应该将其转换为矢量吗?可能还有另一种方法可以在表格中显示它吗?
答案 0 :(得分:0)
实际上错误代码完全没有意义,因为实际错误只是对矩阵的访问:tableModel.addRow(array[1][]);
实际上应该导致第二个括号([]
)的语法错误。要修复语法错误,请将行替换为tableModel.addRow(array[1]);
。一般说明:请格式化您的代码,这是完全不可读的。