June 2017
Beginner
1296 pages
69h 23m
English
... 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.
1 // Fig. 14.18: TokenTest.java
2 // Tokenizing with String method split
3 import java.util.Scanner;
4
5 public class TokenTest {
6 // execute ...Read now
Unlock full access