3

I have a CSV file that has four columns and multiple rows:

a,b,c,d /n

a,b,c,d /n

I successfully read the contents of the .CSV file and placed them into an ArrayList. How can I separate each column into an array? I want to have arrayA, ArrayB, ArrayC, ArrayD.

Scanner scan = new Scanner(new File(copyUri));
    ArrayList<String[]> records = new ArrayList<String[]>();
    String[] record = new String[2];
    while(scan.hasNext())
    {
        record = scan.nextLine().split(",");
        records.add(record);
    }

2 Answers 2

1

Separate them into arrays inside the same loop. I would recommend using an ArrayList if you do not know the number of rows in your csv file.

Scanner scan = new Scanner(new File(copyUri));
ArrayList<String[]> records = new ArrayList<String[]>();
String[] record = new String[2];
ArrayList<String[]> a = new ArrayList<String[]>();
ArrayList<String[]> b = new ArrayList<String[]>();
ArrayList<String[]> c = new ArrayList<String[]>();
ArrayList<String[]> d = new ArrayList<String[]>();

while(scan.hasNext())
{
    record = scan.nextLine().split(",");
    a.add(record[0]);
    b.add(record[1]);
    c.add(record[2]);
    d.add(record[3]);
    records.add(record);
}

Another less efficient solution is finding the size of your records list, creating your arrays using this size and populating them with your data.

Scanner scan = new Scanner(new File(copyUri));
ArrayList<String[]> records = new ArrayList<String[]>();
String[] record = new String[2];
while(scan.hasNext()) {
        record = scan.nextLine().split(",");
        records.add(record);
}
int rows = records.size();
String[] a = new String[rows];
String[] b = new String[rows];
String[] c = new String[rows];
String[] d = new String[rows];
int j = 0;
for (String[] temp : records) {
    a[j] = temp[0];
    b[j] = temp[1];
    c[j] = temp[2];
    d[j] = temp[3];
    j++;
}

A third idea I got is to initialize your arrays with a certain size (ex: 5) and resize the arrays as you go but I believe this is least efficient since you will have to move elements to the new array every time you resize it.

Sign up to request clarification or add additional context in comments.

Comments

0

Use univocity-parsers' ColumnProcessor to do this for you reliably:

CsvParserSettings parserSettings = new CsvParserSettings();

// To get the values of all columns, use a column processor
ColumnProcessor rowProcessor = new ColumnProcessor();
parserSettings.setRowProcessor(rowProcessor);

CsvParser parser = new CsvParser(parserSettings);

//This will kick in our column processor
parser.parse(new FileReader(new File(copyUri)));

//Finally, we can get the column values:
Map<Integer, List<String>> columnValues = rowProcessor.getColumnValuesAsMapOfIndexes();

Clean, faster and easier than doing it by hand. This will handle situations such as rows with different number of columns and add null to the list instead of throwing exceptions at you.

Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.