Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

.csv-file import and export problem

 
Vincent Hendriks
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I have a problem. Hope you can help me.
I am building an faq program with an arraylist of objects (each with variables Question, Answer and Type).
I'm exporting this arraylist to a .csv-file, wich goes OK.

This is the problem:

Since Questions and Answers can contain semicolons (separators for the .csv), I have had to place the Questions and Answers in quotes (' " ' in lines 34 and 35) to make sure everything is displayed correct in Excel.
These quotes don't appear in the excel-sheet , but when I import the same file again, two problems arise:

1 the quotes are made part of the Questions/Answers and
2 when I have semicolons in the Questions/Answers, they screw up the split-method and for instance my Question is half in the question field/half in the answer field and my answer is nowhere to be seen

Is there a way to solve these problems inside my import-/exportcode (I don't want to mess with the windows-separator value in controlpanel)?

These are my import and export methods. Any help/tips are appreciated!

 
Jason Cone
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to ignore semicolons inside quotation marks. Your choices are to come up with a regular expression that will match only semicolons outside of quotation marks and then pass that regex to your split method, or you can get rid of the split method entirely and parse each line, yourself, using indexOf() and substring() inside of a while loop.

Something like the following would work for the "do it yourself" approach (although this one is looking for commas rather than semicolons):

 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CSV is not as trivial to parse as it first looks. I generally advise to use a ready-made library; several are listed in the http://www.coderanch.com/how-to/java/AccessingFileFormats page.
 
Vincent Hendriks
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks both Jason and Lester for your reply.

Jason's suggestion is a bit out of my league at the moment (much stuff unknown to me). I'll have a look at it though: I'l consider it an opportunity to expand my API knowledge
I'll also start on Lester's suggestion. I'm looking for a format to import and export, but not necessarily .csv. Maybe there are better options :-)

So, it's back to the drawing board (tomorrow...my head hurts enough for today )

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic