• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading csv file.  RSS feed

 
Rahul Ba
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am reading one csv file, but my programs seems not working properly.
My file contents are as follows:

Rahul B||Sam C||John
J || Kim K || RAH
P


My progarms is as follows:

I am getting output as which is wrong:

Token:Rahul B
Token:Sam C
Token:John
Token: J
Token: Kim K
Token: RAH
Token: P

I expect output as:

Token:Rahul B
Token:Sam C
Token:John J
Token: Kim K
Token: RAH P

Can anybody help? to solve out the problem.

Rahul.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, why does your csv use pipes as a delimiter and the tokenizer a comma ?

The reason why it does output John and J as two separate tokes is because they appear in two different lines.

When the tokenizer splits the line that contains, there is no J, this comes in the next loop cycle.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Do you mean that always the first comma separated string is always the continuation of the previous line ?

If you assume that , then little logic code will do the trick.


 
Rahul Ba
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry it's my typo mistake .....I replaced comma with ||
 
Rahul Ba
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,
I agree on the Sebastian ..that I read line by line that's why I am getting the output, but I want output as

Token:Rahul B
Token:Sam C
Token:John J
Token: Kim K
Token: RAH P

Is there any open source libarary you are aware of?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why an open source lib ? For what...

Just bring your csv into a format that your code can work with and you are good to go .
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is there any open source libarary you are aware of?


Opensource library are not available to correct human error programs.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only easiest way to solve this is by pasting all lines together first, then use a StringTokenizer (or String.split).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!