• Post Reply Bookmark Topic Watch Topic
  • New Topic

"" missing in csv file  RSS feed

 
Helen Ross
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have created a CSV file with java which takes values the user enters, storing them in variables and then outputting them to the file. The file is being create with the a values being outputed, however it is not placing "" around the values as I would expect for Strings.

The current code i am using is below. Is there something special that needs to be done to display the "" around these values? I am not able to see how escaping them would work as the variable would then be read in as a value (well this is the result I got from trying this).



Thank you in advance.
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A String is represented by " " in Java but when written to a file its just plain text, for that matter any text you write to a file- be it integer, double or string all of them are strings and dont have any type related information. So if you want "string" in the file then you need to add them explicitly. But does the csv file require a ""? It might require if the string itself contains a ,[comma] and you would want the , to be part of the string.
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if I understand your question correctly, but let's give it a shot...

Basically you're just writing text to a text file. Those quotes around the values you write to the file don't just magically appear. You will need to write those yourself; i.e. writer.append("\"");
 
Helen Ross
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So are you saying that I need to do.

writer.append("\"");
writer.append(variable1);
writer.append("\"")
writer.append(",");

If this is the case then this is the one thing i didnt try. I tried adding "\"" into the same statement as the variable1 ie writer.append("\"" variable1 "\""); this is why i was getting confused.
 
Helen Ross
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you that works prefect.
 
Tim Moores
Saloon Keeper
Posts: 4034
94
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CSV is not as simple as it looks at first. I'd use a library that knows the various edge cases to create it; the http://www.coderanch.com/how-to/java/AccessingFileFormats page links to a few different ones.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Helen Ross wrote:So are you saying that I need to do.

writer.append("\"");
writer.append(variable1);
writer.append("\"")
writer.append(",");

If this is the case then this is the one thing i didnt try. I tried adding "\"" into the same statement as the variable1 ie writer.append("\"" variable1 "\""); this is why i was getting confused.



You can write it as a single statement -- but it has to be valid Java code. And that line isn't. Some possible valid lines are....

writer.append("\"" + variable1 + "\"");

or

writer.append("\"").append(variable1).append("\"");


IMO, the first one looks better, but the second one is probably better.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!