... demonstrates String’s split method.

When the user presses the Enter key, the input sentence is stored in variable sentence. Line 14 invokes String method split with the String argument " ", which returns an array of Strings. The space character in the argument String is the delimiter that method split uses to locate the tokens in the String. As you’ll learn in the next section, the argument to method split can be a regular expression for more complex tokenizing. Lines 15–16 display the length of the array tokens—i.e., the number of tokens in sentence. Lines 18–20 output each token on a separate line.

Fig. 14.18

 1  // Fig. 14.18: TokenTest.java 
 2  // Tokenizing with String method split 
 3  import java.util.Scanner; 
 4
 5  public class TokenTest { 
 6     // execute ...

Get Java How to Program, Early Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.