CSV Parsing Database Values CodeNameOne

Viewed 55

GOAL: Realize a CSV Parsing, the code in the documentation works. But I want to edit it. In fact, I want to parse the values of a table called "Concert" I have in my database.

public class Excel extends BaseForm{
  public Excel() throws IOException{
      Form hi = new Form("CSV Parsing", new BorderLayout());

    String[][] data = new String[100][3]; //[7ot max mta3 rows][max mta3 les attributs à afficher]
    int i=0; //compteur mta3 lrows
        ArrayList<Concert> list = ServiceConcert.getInstance().AffichageConcert();
    for (Concert h : list) {
            data[i][0] = h.getName();
            data[i][1] = h.getIdmusician();
            data[i][2] = h.getMusics(); // les attributs lezm ykounou String btw
            i++; // bech tet3ada lel row li baadhha 
        }

    String[] columnNames = new String[data[0].length];
    for(int iter=  0 ; iter < columnNames.length ; iter++) {
        columnNames[iter] = "Col " + (iter + 1);
    }
    System.out.println();
    TableModel tm = new DefaultTableModel(columnNames, data);
    hi.add(BorderLayout.CENTER, new Table(tm));
hi.show();
  }
   

   }

The issue was successfully solved after I've put this code enter image description here

1 Answers

There are several things wrong in the code.

You have for (Concert u : list) { In itself that's fine.

But it ends with hi.add(BorderLayout.CENTER, new Table(tm));

There can be only one component in the center of the form. If you want multiple tables use BoxLayout Y.

This is just a nitpick but:

String[] columnNames = new String[data[0].length];
columnNames[0] = "name";
columnNames[1] = "idmu";
columnNames[2] = "musics";

Just hurts my eyes... Try:

String[] columnNames = { "name", "idmu", "musics" }

And while you're there just define it as a private final field since it doesn't change so it might as well be a constant.

About the core data did you step over it with a debugger?

Is the data physically there? You can inspect the data variable and see the dimensions of the array then see the data went into the array.

If it isn't there then we'll need to see the CSV and pay attention to the line break convention etc. But my main guess is that this line: ServiceConcert.getInstance().AffichageConcert(); doesn't return the full CSV.

Related