-1

I have two arraylists (open to working with other collections) and need to output a csv file that determines whether the contents of these arraylists is the same; cell by cell comparison essentially. The arraylists are taken from a result set and are sorted. They look like this:

sourceData = (9, Orlando, Feb 28, 668, Lloydtown, Dec 1, etc)

targetData = (9, Caledon, Jan 19, 38, South Hark, Dec 1, etc)

What I attempted is to take the first value of the first arraylist, the first value of the second arraylist and then compare them and print that to a CVS file.

for (int i = 0; i <sizeOfData; i++) {
   if (sourceData.get(i).equals(targetData.get(i))){
        validator = ("True");
    } else {
        validator = ("False");
    }
    csvFilePrinter.printRecord(sourceData.get(i), targetData.get(i), validator);
   }

The output looks as desired:

9, 9, true

However, I need the next row in the CSV file to display:

 668, 38, false

so that the overall pattern is:

 9, 9, true, Orlando, Caledon, false, Feb 28, Jan 19, false

 668, 38, false, Lloydtown, South Hark, false, Dec 1, Dec 1, true

where once I put headers, and say the second column is 'city', only city values will be displayed in column 2.

I've tried every which way to write is, but the format of the CSV file is all wrong as every time I use csvFilePrinter, it writes to the next line. How do I write to the next column? When I put what I want in one row into an array I can make this work, but my result sets are going to be different every time so I cannot hardcode how many columns I will need. How can you write across a row, not down a column when writing to a CVS file?

I don't see the solution to my problem in any of the linked posts...

Max B
  • 15
  • 1
  • 6
  • 1
    Not sure if I get you right. Do you want this comparison in one row? CSV is simply coma separated file, so you can simply write comma separated string in a file and save it with .csv extension. And what is `csvFilePrinter` - class, library? – Sergei Sirik Aug 14 '17 at 18:34
  • maybe first build results and then print them. List results; and then for (List row: results ) {csvFilePrinter.printRecord(row);} – John Tribe Aug 14 '17 at 18:37

1 Answers1

0

csvFilePrinter.printRecord seems to print a complete record. In that case, it should be outside the loop. Better to refer, CSVPrinter It can also print Iterable, so it can print ArrayList and the required solution could be as below.

List<String> requiredOutput = new ArrayList<>();
Strin validator = "False";
for (int i = 0; i <sizeOfData; i++) {
   if (sourceData.get(i).equals(targetData.get(i))){
        validator = ("True");
    } else {
        validator = ("False");
    }
    requiredOutput.add(sourceData.get(i));
    requiredOutput.add(targetData.get(i));
    requiredOutput.add(validator);
}
csvFilePrinter.printRecord(requiredOutput);
  • Thanks, this put me on the right path. I wrote another loop to iterate by row and cleared the requiredOutput variable before moving on the the next row. – Max B Aug 15 '17 at 13:44