0

I'm trying to import a .csv file to display in my Java Table. But it shows incomplete data after reaching a certain point. enter image description here

When I change the data to previous ones it displays just fine enter image description here

My csv file looks fine too.. enter image description here

Finally this is how I read the file into the table from a menu item in my interface

        File file = new File("C:\\Users\\User\\Desktop\\Emerging Coursework\\12_book_list_csv.csv");
        try{
            Scanner inputStream = new Scanner(file);
            inputStream.nextLine();
            while (inputStream.hasNextLine()) {                    
                String data = inputStream.nextLine();
                String[] values = data.split(",");
                
                for(int i = 0; i < values.length; i++){
                    System.out.println(values[i]);
                }
                int rowCount = browseTable.getRowCount();
                System.out.println("rowCount: " + rowCount);
                int columnCount = browseTable.getColumnCount();
                System.out.println("columnCount: " + columnCount);
                int nextRow = 0;
                boolean emptyRowFlag = false;
                String testValue;
                do {
                    testValue = (String) browseTable.getValueAt(nextRow, 0);
                        if (testValue != null && testValue.length()!=0) {
                            System.out.println("testvalue:" + testValue);
                            System.out.println("testValue.length" + testValue.length());
                            nextRow++;
                        } else{
                        emptyRowFlag = true;
                        }            
                    } while(nextRow < rowCount && !emptyRowFlag);

                    for (int i = 0; i < columnCount; i++){
                        browseTable.setValueAt(values[i], nextRow, i);
                    } 
            }
        } catch(Exception e){
            
        }
Samit Paudel
  • 181
  • 7
  • 3
    1) Don't post images of textual data. Add the data itself, preferably hard coded into a.. 2) ..[mre] we can run. 3) BTW - don't add the IDE tag unless you test it in another IDE and the problem disappears. That would indicate the problem is with the IDE rather then the Java code. I'm *almost certain* it's the code, not the environment. – Andrew Thompson Nov 19 '20 at 08:04
  • 3
    *I'm trying to import a .csv file* - Just read the file one line at a time and add the data to the TableModel. The basic logic should be 1) read the first line of data. 2) split the data into an array of column names 3), Create an empty DefaultTableModel using the column names array. 4) read another line of data, 5) split the line into an array of data 6) add the array of data to the DefaultTableModel using the addRow() method. 7) repeats steps 4-6 until no more data. Not sure why you have boolean values and are using the setValueAt() method. – camickr Nov 19 '20 at 15:04
  • 1
    .. 4) `catch(Exception e){ }` Don't ignore exceptions! They inform us exactly what went wrong. Unless logging is implemented, at least call `Throwable.printStackTrace()` 5) I'm closing this tab in the browser now, so if you need my further attention.. Tip: Add @camickr (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Nov 21 '20 at 07:02

0 Answers0