2015-05-19 96 views
1

阵列到阵列的阵列面临问题。我的程序将表格数据保存为单独的文本文件(每天单独的文件)。现在我想阅读所有内容并对每个字段进行求和。 我保存的文本文件看起来像:阵列阵列到表

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 

我已经manged方法来读取并保存数值到数组[] []。但现在我需要将这些数组打印到表格中。

这是我从文件中读取代码:

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不能转换为矢量

但我应该将其转换为矢量?可能有另一种方法在表格中显示它?

回答

0

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

+0

没有第二个括号它是相同的错误代码。 – Vilhis

+0

有一些代码缺失,但我猜'tableModel'类型'AbstractTableModel',而不是'DefaultTableModel' – Paul

+0

所以我想要做的就是以某种方式显示我的数组[] []到表。是否有必要将所有我的'int数组'按字段转换为向量或目标表字段,还是有另一种方式来显示它? – Vilhis