Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tokenize problem in Java with separator ". "

I need to split a text using the separator ". ". For example I want this string :

Washington is the U.S Capital. Barack is living there.

To be cut into two parts:

Washington is the U.S Capital. 
Barack is living there.

Here is my code :

// Initialize the tokenizer
StringTokenizer tokenizer = new StringTokenizer("Washington is the U.S Capital. Barack is living there.", ". ");
 while (tokenizer.hasMoreTokens()) {
      System.out.println(tokenizer.nextToken());

}

And the output is unfortunately :

Washington
is
the
U
S
Capital
Barack
is
living
there

Can someone explain what's going on?

like image 676
poiuytrez Avatar asked Jun 04 '10 07:06

poiuytrez


People also ask

How do you Tokenize a sentence in Java?

Example of hasMoreTokens() method of the StringTokenizer class. This method returns true if more tokens are available in the tokenizer String otherwise returns false. The above Java program shows the use of two methods hasMoreTokens() and nextToken() of StringTokenizer class.

How do I use StringTokenizer with multiple delimiters?

In order to break String into tokens, you need to create a StringTokenizer object and provide a delimiter for splitting strings into tokens. You can pass multiple delimiters e.g. you can break String into tokens by, and: at the same time. If you don't provide any delimiter then by default it will use white-space.

What is the default delimiter of StringTokenizer?

Constructs a string tokenizer for the specified string. The tokenizer uses the default delimiter set, which is " \t\n\r\f" : the space character, the tab character, the newline character, the carriage-return character, and the form-feed character.

Is string tokenizer faster than split?

The split() method is preferred and recommended even though it is comparatively slower than StringTokenizer. This is because it is more robust and easier to use than StringTokenizer.


2 Answers

Don't use StringTokenizer; it's a legacy class. Use java.util.Scanner or simply String.split instead.

    String text = "Washington is the U.S Capital. Barack is living there.";
    String[] tokens = text.split("\\. ");
    for (String token : tokens) {
        System.out.println("[" + token + "]");
    }

This prints:

[Washington is the U.S Capital]
[Barack is living there.]

Note that split and Scanner are "regex"-based (regular expressions), and since . is a special regex "meta-character", it needs to be escaped with \. In turn, since \ is itself an escape character for Java string literals, you need to write "\\. " as the delimiter.

This may sound complicated, but it really isn't. split and Scanner are much superior to StringTokenizer, and regex isn't that hard to pick up.

Regular expressions tutorials

  • Java Lessons/Regular expressions
  • regular-expressions.info - Very good tutorial, not Java specific

Related questions

  • Scanner vs. StringTokenizer vs. String.Split

API Links

  • java.util.StringTokenizer
    • StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
  • java.util.Scanner
    • A simple text scanner which can parse primitive types and strings using regular expressions.
    • Java Tutorials - Basic I/O - Scanning and formatting
  • String[] String.split
    • Splits this string around matches of the given regular expression.

But what went wrong?

The problem is that StringTokenizer takes each character in the delimiter string as individual delimiters, i.e. NOT the entire String itself.

From the API:

StringTokenizer(String str, String delim): Constructs a string tokenizer for the specified string. The characters in the delim argument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens.

like image 115
polygenelubricants Avatar answered Oct 09 '22 12:10

polygenelubricants


Your StringTokenizer constructor takes the delimiter ". " which matches dot or space as delimiters.

like image 39
krock Avatar answered Oct 09 '22 12:10

krock