user550738
user550738

Reputation:

how to parse empty tokens from a comma-separated data?

I have some large files with comma separated data in them. Something like:

firstname,middlename,lastname
James,Tiberius,Kirk
Mister,,Spock
Leonard,,McCoy

I'm using a StringTokenizer to parse the data:

StringTokenizer st = new StringTokenizer(sLine, ",");
while (st.hasMoreTokens()) {
  String sTok = st.nextTokens;
  tokens.add(tok);
}

The problem is, on lines with no middle name, I only get two tokens, { "Mister", "Spock" }, but I want three tokens, { "Mister, "", "Spock" }

QUESTION: How do I get empty tokens included when parsing my comma separated data?

Thanks!

Upvotes: 0

Views: 100

Answers (4)

Omar Hrynkiewicz
Omar Hrynkiewicz

Reputation: 498

Consider the use of Splitter of Guava Splitter

And you can create an splitter with or without omit empty Strings.

//Example without omit empty Strings (default)
Splitter splitterByComma = Splitter.on(",");
Iterable<String> split = splitterByComma.split("Mister,,Spock");

//Example omitting empty Strings
Splitter splitterByComma = Splitter.on(",").omitEmptyStrings();
Iterable<String> split = splitterByComma.split("Mister,,Spock");

Upvotes: 0

Jotakun
Jotakun

Reputation: 421

Use split(",") instead of a StringTokenizer:

String[] aux = sLine.split(",");
for(int i = 0; i < aux.length; i++) {
    String sTok = aux[i];
    tokens.add(sTok);
}

You can see in the documentation that StringTokenizer is a legacy class and is only kept for retro-compatibility: http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html

Upvotes: 1

Kakalokia
Kakalokia

Reputation: 3191

Use split method, but pass -1 as the second argument to keep empty strings

sLine.split(",", -1);

Upvotes: 0

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

You can use the String#split(String regex) method.

String[] split = sLine.split(",");
for (String s : split) {
    System.out.println("S = " + s); //Note there will be one empty S
    tokens.add(s);
}

Upvotes: 2

Related Questions