2015-05-19 100 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