1

I have written a function which creates and writes into CSV file. My function is as follow:

private static void writeIntoCSV(GaData results, String filename){

    try{
        File f = new File("D:\\" + filename);
        BufferedWriter output = new BufferedWriter(new FileWriter(f));

        int i = 0;
        for (ColumnHeaders header : results.getColumnHeaders()) {
            i++;
            if(i < header.size()){
                output.write(header.getName() + ",");
                i++;
            }
            else{
                output.write(header.getName());
            }
        }
        output.write("\n");
        int j = 0;
        for (List<String> row : results.getRows()) {
            j++;
            for (String column : row) {
                if(j < row.size()){
                    output.write(column + ",");
                    j++;
                }
                else{
                    output.write(column);
                }
            }
            output.write("\n");
            j = 0;
        }

        output.close();
    }
    catch(Exception e){
        System.out.println(e);
    }
}

Here when I have two headers its seperating it properly like a,b but when I have three headers its separating as: a,bc

I tried to change the the value and place of i like:

int i = 1;
for (ColumnHeaders header : results.getColumnHeaders()) {
    if(i < header.size()){
        output.write(header.getName() + ",");
        i++;
    }
    else{
        output.write(header.getName());
    }
}
output.write("\n");

In this case its showing headers like a,b, for two headers and a,b,c for three headers. How can I resolve this to place comma properly.

For data rows its working properly.

Edi G.
  • 2,432
  • 7
  • 24
  • 33
Maddy
  • 105
  • 1
  • 5
  • 2
    Why don't you use a library to write CSV? That will also take care of nasty things like commas inside of data values. http://stackoverflow.com/questions/101100/csv-api-for-java?rq=1 – Thilo Apr 22 '15 at 11:01
  • Does your ColumnHeader#size() method actually return the value you think it does? It may be off by +/-1. – Roger Gustavsson Apr 22 '15 at 11:06
  • In-correct code. what is `header.size()` returning? Is it returning total number of headers or size of header name? – Aakash Apr 22 '15 at 11:09

4 Answers4

1

You can use Guava Joiner to join on ',' something like this:

 Joiner joiner = Joiner.on(",").skipNulls();
 return joiner.join("Harry", null, "Ron", "Hermione");

full documention Guava docs

igreenfield
  • 1,618
  • 19
  • 36
1

What is returning value of calling to header.size()? Is it header column count? As my viewpoint, I think you should compare with the size of results.getColumnHeaders() instead of header.size()

Duc Vo
  • 61
  • 3
1

Remove the first increment for I. Simple way to look at is this. There are three headers. I is set to 0. You start the loop, i becomes 1. I is less than 3, so the first block is executed. You increment again, I is now 2. The loops wraps around. I is incremented again to 3. But you are only on the second value.

Basically you are incrementing I to many times!

OhDearMoshe
  • 39
  • 1
  • 5
0

Set your i in the second code to 0 not to 1 and it should work

CodeFox
  • 447
  • 4
  • 12