StringTokenizer Class (Legacy)
Before String.split existed (it arrived in Java 1.4), the way to break a line like "Java,is;fun" into words was StringTokenizer from java.util. You'll still meet it in older code and in fast-input templates used for competitive programming, so it's worth recognizing. For new code, though, split is clearer, and the Javadoc itself says StringTokenizer is kept only for compatibility.
Tokenizing a Line
Tokens.java▶ Try it
import java.util.Arrays;
import java.util.StringTokenizer;
public class Tokens {
public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("Java,is;fun", ",;");
System.out.println(st.countTokens() + " tokens");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
String line = "Navika,,Srihitha,";
System.out.println(Arrays.toString(line.split(",")));
StringTokenizer st2 = new StringTokenizer(line, ",");
System.out.println(st2.countTokens() + " tokens from StringTokenizer");
}
}Output
3 tokens
Java
is
fun
[Navika, , Srihitha]
2 tokens from StringTokenizerHow it works
- The second argument,
",;", is a set of delimiter characters: any comma or semicolon splits. hasMoreTokens()andnextToken()walk through the pieces one at a time, like an iterator.- Empty pieces are skipped. In
"Navika,,Srihitha,",StringTokenizersees only 2 tokens, whilesplitkeeps the empty piece between the two commas.
StringTokenizer Compared with split
StringTokenizer | String.split | |
|---|---|---|
| Delimiters | A set of single characters | A regular expression, such as "\\s+" or ", " |
| Empty pieces | Skipped | Kept (trailing empties dropped unless you pass -1) |
| Result | Read one token at a time | An array of all pieces |
| Status | Legacy, kept for compatibility | Standard |
Common Mistakes
Expecting empty fields from StringTokenizer. CSV-style data with blank fields loses them; use split(",", -1).
Passing a multi-character delimiter like ", ". It's treated as two separate delimiter characters, , and space, not as the pair.
Using it in new code. split is clearer and handles patterns.
Practice
- Easy: Use
StringTokenizerto count the words in"Advitha loves Java", then do the same withsplit("\\s+")afterstrip(). - Harder: Parse
"Pune:12;Delhi:30;Chennai:24"into city and temperature pairs, first withStringTokenizerand then with twosplitcalls.
Key Takeaways
Was this tutorial helpful?
Lesson 203 of 482