-1

I have a csv file saved with the name - "public/csvFiles/pushup.csv", where I have converted a 2D matrix into csv format. Now I want to make a static method in a manager class, where in I will make a 2D array by fetching data from that csv file. So I want to know how to do that. How to make a 2D array from the data present in the csv file?

I have made my csv file in the following format :

  • comma separated values, after every comma, means new column
  • every new line means new row
Rehmanali Momin
  • 199
  • 1
  • 1
  • 14
  • 7
    Possible duplicate of [Converting CSV File Into 2D Array](https://stackoverflow.com/questions/33034833/converting-csv-file-into-2d-array) – meditat Dec 04 '17 at 14:00
  • 2
    Use a CSV parser library, like [OpenCSV](http://opencsv.sourceforge.net/) or [Apache Commons CSV](https://commons.apache.org/proper/commons-csv/). – Olivier Grégoire Dec 04 '17 at 14:14
  • 1
    Questions asking for *homework help* **must** include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it ([help], [ask]). Also show some **research effort** before posting a question, thanks. – Zabuzard Dec 04 '17 at 15:46

1 Answers1

-1

If I get you right - the first dimension of an array is structure reflecting lines. Second dimension - rows of CSV file.

So I propose to create a structure reflecting row. Names must be meaningful. But the idea is:

RowClss {
    String row;
    String row 2;
    // etc
}

Then in the loop, you can read the file and fill the array:

String[x][y] rows = new String[x][y];

for(ont i = 0; i < file.size; i++) {
    // read line from file and parse it here
    for(j = 0; j < numOfColumns;j++) {
        rows[j][i] = new String(valueOfCurrentCollumn);
    }
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103