我有一个从另一个程序生成的文件,我正在尝试创建一个其他程序,该程序将创建一个包含文件的JTable。我已经尝试使用StackOverflow教程对其进行修复,但是我无法弄清楚出了什么问题。我使用了一个教程来帮助我创建JTable,因此我什至无法弄清楚它出了什么问题。
这是文件的内容
|10.140000| 13.084819| 4392.000000| 82.699923| 11.900000| 10.118987| 5166.000000| 59.443868|
|28.880000| 8.831744| 48346.000000| 583.104062| 32.200000| 5.957555| 4264.000000| 93.116301|
|259.900000| 3.492032| 22688.000000| 126.516054| 267.300000| 1.961500| 20010.000000| 66.324373|
|624.020000| 2.592010| 14994.000000| 30.505162| 632.360000| 1.244278| 15406.000000| 19.590889|
|1886.800000| 1.147785| 52436.000000| 30.082485| 1896.840000| 0.683268| 53950.000000| 95.582836|
|4293.860000| 0.919986| 226210.000000| 134.282595| 4294.760000| 0.392905| 77470.000000| 28.020386|
|9577.540000| 0.400449| 424068.000000| 116.602770| 9577.140000| 0.206953| 184788.000000| 117.112959|
|129563.160000| 0.125037| 1203728.000000| 6.114531| 129205.700000| 0.054201| 1386360.000000| 6.121366|
|764396.660000| 0.051151| 8170482.000000| 12.860777| 762439.080000| 0.023297| 10045558.000000| 42.648308|
|1628690.480000| 0.028820| 20205582.000000| 34.315799| 1624935.860000| 0.012787| 23381164.000000| 23.840969|
这是代码中填充值的部分:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String filePath = "C:\\Users\\hafiz\\OneDrive\\Documents\\test\\file.txt";
File file = new File(filePath);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String[] columnNames = {
"Size", "Iterative Avg Count", "Iterative Coef Count",
"Iterative Avg Time", "Iterative Coef Time", "Recurive Avg Count",
"Recurive Coef Count", "Recurive Avg Time", "Recurive Coef Time"
};
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.setColumnIdentifiers(columnNames);
Object[] tableLines = br.lines().toArray();
for (int i = 0; i < tableLines.length; i++) {
String line = tableLines[i].toString().trim();
String[] dataRow = line.split("|");
model.addRow(dataRow);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Table.class.getName()).log(Level.SEVERE, null, ex);
}
}
这是完整的代码,如果有任何用处:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author hafiz
*/
public class Table extends javax.swing.JFrame {
/**
* Creates new form Table
*/
public Table() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
}
));
jScrollPane1.setViewportView(jTable1);
jButton2.setText("Import Text");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 745, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(331, 331, 331)
.addComponent(jButton2)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String filePath = "C:\\Users\\hafiz\\OneDrive\\Documents\\test\\file.txt";
File file = new File(filePath);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String[] columnNames = {
"Size", "Iterative Avg Count", "Iterative Coef Count",
"Iterative Avg Time", "Iterative Coef Time", "Recurive Avg Count",
"Recurive Coef Count", "Recurive Avg Time", "Recurive Coef Time"
};
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.setColumnIdentifiers(columnNames);
Object[] tableLines = br.lines().toArray();
for (int i = 0; i < tableLines.length; i++) {
String line = tableLines[i].toString().trim();
String[] dataRow = line.split("|");
model.addRow(dataRow);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Table.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Table().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
第一列(大小)对于值记录为空。
您有两个主要问题:
您输入的| 10.140000 | 13.084819 | 4392.000000 | 82.699923 | 11.900000 | 10.118987 | 5166.000000 | 59.443868 |以分隔符开头,因此size列将为空,因为它将包含第一个|左边的子字符串。实例
您的输入有8个值,但是您有9列,因此至少一列将没有值。