1

I am trying to write 2 different arrays to a csv. The first one I want in the first column, and second array in the second column, like so:

array1val1   array2val1
array1val2   array2val2

I am using the following code:

String userHomeFolder2 = System.getProperty("user.home") + "/Desktop";
String csvFile = (userHomeFolder2 + "/" + fileName.getText() + ".csv");
FileWriter writer = new FileWriter(csvFile);

final String NEW_LINE_SEPARATOR = "\n";
FileWriter fileWriter;
CSVPrinter csvFilePrinter;
CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);

fileWriter = new FileWriter(fileName.getText());
csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);

try (PrintWriter pw = new PrintWriter(csvFile)) {

    pw.printf("%s\n", FILE_HEADER);

    for(int z = 0; z < compSource.size(); z+=1) {
        //below forces the result to get stored in below variable as a String type

        String newStr=compSource.get(z);
        String newStr2 = compSource2.get(z);

        newStr.replaceAll(" ", "");
        newStr2.replaceAll(" ", "");

        String[] explode = newStr.split(",");
        String[] explode2 = newStr2.split(",");

        pw.printf("%s\n", explode, explode2);
    }
}
catch (Exception e) {
    System.out.println("Error in csvFileWriter");
    e.printStackTrace();

} finally {
    try {
        fileWriter.flush();
        fileWriter.close();
        csvFilePrinter.close();
    } catch (IOException e ) {
        System.out.println("Error while flushing/closing");
    }
}

However I am getting a strange output into the csv file:

[Ljava.lang.String;@17183ab4

I can run

pw.printf("%s\n", explode);
pw.printf("%s\n", explode2);

Instead of : pw.printf("%s\n", explode, explode2); and it prints the actual strings but all in one same column.

Does anyone know how to solve this?

dgelinas21
  • 641
  • 3
  • 9
  • 22
  • https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array – Mordechai Jul 14 '17 at 15:37
  • Could you please show content of compSource and coupSource2? Moreover, what is the need of FileWriter writer, CSVPrinter csvFilePrinter and CSVFormat csvFileFormat? You just use a PrintWriter. – OlivierTerrien Jul 14 '17 at 16:03

3 Answers3

3

1.Your explode and explode2 are actually String Arrays. You are printing the arrays and not the values of it. So you get at the end the ADRESS of the array printed. You should go through the arrays with a loop and print them out.

for(int i = 0; i<explode.length;++i) {
pw.printf("%s%s\n", explode[i], explode2[i]);
}

2.Also the method printf should be look something like

pw.printf("%s%s\n", explode, explode2);

because youre are printing two arguments, but in ("%s\n", explode, explode2) is only one printed.

Try it out and say if it worked

Superluminal
  • 947
  • 10
  • 23
0

After these lines:

newStr.replaceAll(" ", "");
newStr2.replaceAll(" ", "");

String[] explode = newStr.split(",");
String[] explode2 = newStr2.split(",");

Use this code:

int maxLength = Math.max(explode.length, explode2.length);
for (int i = 0; i < maxLength; i++) {
  String token1 = (i < explode.length) ? explode[i] : "";
  String token2 = (i < explode2.length) ? explode2[i] : "";

  pw.printf("%s %s\n", token1, token2);
}

This also cover the case that the arrays are of different length.

Ossin Java guy
  • 365
  • 3
  • 12
0

I have removed all unused variables and made some assumptions about content of compSource. Moreover, don't forget String is immutable. If you just do "newStr.replaceAll(" ", "");", the replacement will be lost.

public class Tester {

    @Test
    public void test() throws IOException {

        // I assumed compSource and compSource2 are like bellow
        List<String> compSource = Arrays.asList("array1val1,array1val2");
        List<String> compSource2 = Arrays.asList("array2val1,array2val2");

        String userHomeFolder2 = System.getProperty("user.home") + "/Desktop";
        String csvFile = (userHomeFolder2 + "/test.csv");

        try (PrintWriter pw = new PrintWriter(csvFile)) {

            pw.printf("%s\n", "val1,val2");

            for (int z = 0; z < compSource.size(); z++) {

                String newStr = compSource.get(z);
                String newStr2 = compSource2.get(z);

                // String is immutable --> store the result otherwise it will be lost
                newStr = newStr.replaceAll(" ", "");
                newStr2 = newStr2.replaceAll(" ", "");

                String[] explode = newStr.split(",");
                String[] explode2 = newStr2.split(",");

                for (int k = 0; k < explode.length; k++) {
                    pw.println(explode[k] + "\t" + explode2[k]);
                }
            }
        }
    }
}
OlivierTerrien
  • 2,451
  • 1
  • 19
  • 31