0

I have 7 arrays and I want each array to be a column in a CSV file. I'm currently using OpenCSV. But, I haven't figured out a way to generate a CSV file from multiple arrays.

String[] Title = new String[1000];
String[] Starring = new String[1000];
String[] Director = new String[1000];
CSVWriter writer = new CSVWriter(new FileWriter("export.csv"), ',');
String[] entries = Title[1] + Starring[1].split('#');
writer.writeNext(entries);
dur
  • 15,689
  • 25
  • 79
  • 125
Grayson
  • 17
  • 4

3 Answers3

0

Assuming you have a 2D array of String, here's what you can do:

CSVWriter writer = //get csv writer
String[][] lines = //lines
for(String[] lines : lines){
    writer.writeNext(lines);
}
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
0

The writeNext method in openCSV takes an array of String with each element being the value if you have separate arrays you have to build the single array to feed the writeNext. I am not sure if this will work but its worth a shot.

private String[] createRowFromSeparateArrays(String... columns) {
   return columns;
}

And then in your code you would have

for (int i = 0; i < Title.length; i++) {
 writer.writeNext(createRowFromSeparateArrays(Title[i],Starring[i],Director[i]));
}

good luck :)

Scott Conway
  • 975
  • 7
  • 13
-1

You first need to create parallel arrays and cast them into String then Parse the actual array toString. GOOD LUCK