3

I've written a Java class to generate CSV file :

public class CSVGenerator {

    private static String REFERRAL_HEADER = "Position,Mail,Invitations,Qualified invitations";
    private static String TOPS_HEADER = "Position,Mail,Number of conferences,Number of new contacts, Average conf duration";

    public static String generate(String source, JsonNode root) throws IOException {
        String result = "";
        CSVWriter writer = new CSVWriter(new FileWriter(source+".csv"));
        if (source.compareTo("referral") == 0)
            result = REFERRAL_HEADER;
        else if (source.compareTo("tops") == 0)
            result = TOPS_HEADER;
        writer.writeNext(result.split(","));
        Iterator<JsonNode> it = root.getElements();
        while (it.hasNext()) {
            Iterator<JsonNode> it2 = it.next().getElements();
            result = "";
            while (it2.hasNext())
            {
                result += it2.next().asText();
                if (it2.hasNext())
                    result += ",";
            }
            writer.writeNext(result.split(","));
        }
        writer.close();
        return result;
    }
}

The resulting file isn't well formated. There's only one cell by row, containing all the cell and the separating comma like this :

header1,header2,header3
1,support,test
2,alpha,gamma

What am I doing wrong ?

EDIT:

To be more precise on the output

I have : "header1,header2,header3","",""

I want : "header1","header2","header3"

Here's my output. Each row in one unique cell.

   ""Position","Mail","Invitations","Qualified invitations""
    ""1","xxxxx.com","129","23""
    ""2","xxxx.com","54","8""
    ""3","xxxx.com","197","5""
    ""4","xxxx","13","5""
    ""5","xxxx","8","4""
    ""6","xxxx.com","4","4""
    ""7","xxx.com","31","3""
    ""8","xxx.com","30","3""
    ""9","xxx.com","18","3""
    ""10","xxx.com","13","3""
Raedwald
  • 46,613
  • 43
  • 151
  • 237
Yabada
  • 1,728
  • 1
  • 15
  • 36
  • What's wrong with that output? looks okay to me – Marco Forberg Jul 19 '13 at 15:01
  • I won't put emails and stuff here. The problem is that there is one cell for one row. It's like the comma separation doesn't work. So I have : "header1,header2,header3","","" instead of "header1","header2","header3" – Yabada Jul 19 '13 at 15:07
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Mar 14 '18 at 00:52

4 Answers4

-1

Maybe this would be better. Cheers!

result += \"+data.asText()+\";
RobEarl
  • 7,862
  • 6
  • 35
  • 50
Sadbrute
  • 30
  • 7
-1

Did you try putting quotes around the result attribute;

result += "\""+data.asText()+"\"";

Bearing in mind I'm not familiar with jsondata object.

Also you should write each line by line rather than appending everything at the end.

And yes, have a look at the builtin CSVReader and CSVWriter.

WattoWatto
  • 159
  • 8
-1

Try adding quotes around the header fields:

private static String REFERRAL_HEADER = "\"Position\",\"Mail\",\"Invitations\",\"Qualified invitations\"\n";
private static String TOPS_HEADER = "\"Position\",\"Mail\",\"Number of conferences\",\"Number of new contacts\", \"Average conf duration\"\n";
Rob Smith
  • 434
  • 4
  • 3
  • I tried, since i use CSVWriter it just result in ""Position"" because CSVWriter already put quotes – Yabada Jul 19 '13 at 15:47
-1

I tried a subset of your code in a groovy shell and I got correct results:

groovy:000> string = new java.io.StringWriter()
===> 
csv = new CSVWriter(string)
===> au.com.bytecode.opencsv.CSVWriter@4d9ac0b4
groovy:000> csv.writeNext(REFERRAL_HEADER.split(","))
===> null
groovy:000> string.toString()
===> "Position","Mail","Invitations","Qualified invitations"

You might try using commons-csv instead of that CSVWriter. That library is a little funky because it is tailored to databases.

CaTalyst.X
  • 1,645
  • 13
  • 16