This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem to reading CSV file with comma seperated field  RSS feed

 
signor Rhena
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading CSV file with comma seperated field
eg:
123,abc,1234abs,12.34
.....................
...................
321,,5436dvc,87.00
----------------------------
There might be null too in some field like second field in last line!!

When I am reading it escape the null and jump into next field.
When reading
321,,5436dvc,87.00
it reads
321,5436dvc,87.00
I am using this code
public List readDataFile (String inputFileName) throws IOException {

FileReader i nputFileReader = new FileReader(inputFileName);
BufferedReader inputStream = new BufferedReader(inputFileReader);
List memberList = new ArrayList();

String inLine, valueString, s = null;

try {
int count = 0;
while ((inLine = inputStream.readLine()) != null) {

MembersVo memb = new MembersVo();
String delims = ",";
StringTokenizer st = new StringTokenizer(inLine, delims,false);
String var2write = null;

while (st.hasMoreElements()) {
s = (String) st.nextElement();
valueString = s.trim();

switch (count) {
case 0 :memb.setRegID(Integer.parseInt(valueString));
break;
case 1 :
memb.setX(valueString);
break;
case 2 :
memb.setY(valueString);
break;
case 3 :
memb.setZ(valueString);
break;
default :
break;
}
count++;
}
memberList.add(memb);
}
} catch (FileNotFoundException e) {
System.out.println(" Error : " + e);
}

return memberList;
}

Whats wrong ?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's more to CSV parsing than what you have done so far. It might be easier to just use soemeone else's CSV code instead of trying to roll your own. Search for "Ostermiller Utils"; it contains a capable CSV parser.
 
signor Rhena
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got the solution
if comma are saparated with space it work if not it won't

123,acc,,ws12
reads like
123,acc,ws12

but
123,acc, ,ws12
reads like
123,acc, , ws12

Still don't know how to read null if there is no space between comma ?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get your code going, make that last parameter on StringTokenizer true. Then it will give you the commas as well as the values. You'll have to count commas to know which field you're in.

But to process more complex CSV you have other problems. Most sources of CSV put strings in quotes. They can include commas inside the quotes, and your StringTokenizer will incorrectly split that into two fields. They can also include quotes inside the quotes with some kind of escaping mechanism.

I'd take the advice above and look around for a solid implementation that has been around and tested for a while.
 
Casper Maxwell
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may also just add a preprocessor to your CSV parser. It should convert ",," instances into ", ,". But as said before, it will not work with a value like "A ,, String". It will also change that value into "A , , String".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!