1

What if I have a file that I am using a string tokenizer on to get values between commas. Its a csv file. Here is sample input:

test,first,second,,fourth,fifth

so how can i catch that empty comma? Right now its just pretending nothing is there. It doesn't even see that there is a place with nothing in it.

Zach
  • 23
  • 4
  • can you post the tokenizer code? – Karthik Ramachandran May 03 '11 at 15:41
  • 1
    First: is this homework or a real-life scenario? That makes a huge difference in how the question will be answered – Sean Patrick Floyd May 03 '11 at 15:43
  • possible duplicate of [CSV parsing in Java - working example..?](http://stackoverflow.com/questions/843997/csv-parsing-in-java-working-example) –  May 03 '11 at 15:53
  • CSV parsing is way more complicated that it first looks like. Use a CSV parsing library, there are lots on the internet to choose from. –  May 03 '11 at 15:54
  • its real life. i needed a quick and dirty way of parsing the csv file. I didnt need a heavyweight solution so the first thing that popped into my mind was a tokenizer but the split works much better and probably less overhead. – Zach May 04 '11 at 14:57

3 Answers3

4

Using String#split() would be recommended over StringTokenizer.

String[] s = "test,first,second,,fourth,fifth".split(",");
System.out.println(Arrays.asList(s));
System.out.println(s.length);

// output:
// [test, first, second, , fourth, fifth]
// 6

Also, if you have much more involved CSV parsing in your code, if possible, try using an existing library like JavaCSV.

Sanjay T. Sharma
  • 22,857
  • 4
  • 59
  • 71
1

I am not sure if I am understanding your question correctly. I would use well-known packages like opencsv.

Tom Duckering
  • 2,727
  • 1
  • 23
  • 27
Luixv
  • 8,590
  • 21
  • 84
  • 121
0

The split technique works great, so long as none of your elements have a comma inside it. You can use existing libraries. I've also had good results using regexp for CSV processing.

Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52