-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
3
  • 7
    Possible duplicate of Converting CSV File Into 2D Array Commented Dec 4, 2017 at 14:00
  • 2
    Use a CSV parser library, like OpenCSV or Apache Commons CSV. Commented Dec 4, 2017 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 center, How to Ask). Also show some research effort before posting a question, thanks. Commented Dec 4, 2017 at 15:46

1 Answer 1

-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);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Use comments to ask for clarifications. Until you have high enough reputation, focus on questions you can answer without asking for clarification.
Thank you. Get it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.