BreakIterator vs. StringTokenizer for delimited file with null values

Hi! I am working with a delimited file that contains null values (looking like ,,). I am using the String

Tokenizer which works fine as long as there are no null values. Once it hits a null, it just skips right over the nulls (not counting it as a token), and causes and Exception to be thrown at the end due to the .nextToken() method not finding the correct amount of tokens.

For example, if the delimited file is:

Number1,Number2,,Number3

If I use the nextToken() method, if will print:

Number1

Number2

Number3

Exception thrown as can't find right number of tokens

I have tried to use the BreakIterator.getWordInstance() however the commas still print out - Which is exactly what the StringTokenizer does when the bReturnTokens = true. I have seen a lot of forums on customizing the StringTokenizer, but I am wondering if there is a way to use the BreakIterator class for this?

Thanks for the help!

[979 byte] By [angesim] at [2007-9-26 4:12:27]
# 1

if you generates the original file with lines such as "Number1,Number2,,Number3",

i suggest you to write a space character before each coma => "Number1 ,Number2 , ,Number3"

now the StringTokenizer will work !

you can test with this code :

import java.util.StringTokenizer;

public class Test {

private static void getNumbers(String p_text) {

StringTokenizer tok = new StringTokenizer(p_text,",");

String numberString;

Integer number;

while(tok.hasMoreTokens()) {

numberString = tok.nextToken().trim();

if (numberString.length() > 0) {

number = new Integer(numberString);

}

else {

number = null;

}

System.out.println(number);

}

}

public static void main(String [] args) {

getNumbers("3 ,2 , ,1");

}

}

angifredf at 2007-6-29 13:18:20 > top of Java-index,Archived Forums,Java Programming...