-1

This is how I get data from csv-file:

 cSVFileReader = new CSVReader(new FileReader(sciezka), ','); // csv reader with coma-separator
 java.util.List<String[]> myEntries = cSVFileReader.readAll();
 String[][] rowData = myEntries.toArray(new String[0][]);
 rowData = myEntries.toArray(new String[0][]);
 columnnames = myEntries.get(0);
 rowData = myEntries.toArray(new String[0][]);
 DefaultTableModel tableModel = new DefaultTableModel(rowData, columnnames);
 JTable table =  new JTable(tableModel);
 return table;

and this is how I count average:

    public void getAverage() throws IOException{
    CSVFile table = new CSVFile() ;
    float sum = 0;
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    int column = table.getSelectedColumn();
    System.out.println(column); //show -1
    rowcount = model.getRowCount();
    System.out.println(rowcount); //show zero
}

I think, the problem is in wrong getting TableModel from JTable, but actually I can't understand how can I do it another way.

Kat Roz
  • 59
  • 9
  • 1
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code 3 table rows to factor out the CSV file. – Andrew Thompson Aug 22 '16 at 11:18
  • 1
    `I think, the problem is in wrong getting TableModel from JTable,` - code looks reasonable to me so the question is do you have any data in the table. Do some basic debugging and print out each value as you access it using the getValueAt(...) method. Then you will know if your code is being executed. – camickr Aug 22 '16 at 14:02
  • Thanks for letting me know about that, @andrew-thompson , next time I will do my best :) – Kat Roz Aug 22 '16 at 17:48
  • 1
    @KatiDanaRozanova, `next time I will do my best` - there is no time like the present. If you still have a problem, then update the question with the MCVE. If the problem is solved, then state that so people don't spend time on an answered question. – camickr Aug 22 '16 at 19:20

1 Answers1

1

So finally it works. My solution is table, which is passed as a parameter

public float getAverage(JTable table) throws IOException
Kat Roz
  • 59
  • 9