Java Reference
In-Depth Information
Using the Scanner class
The Scanner class is used to read data from a text source. This might be standard input or
it could be from a file. It provides a simple-to-use technique to support tokenization.
The Scanner class uses whitespace as the default delimiter. An instance of the Scanner
class can be created using a number of different constructors. The constructor in the follow-
ing sequence uses a simple string. The next method retrieves the next token from the in-
put stream. The tokens are isolated from the string, stored into a list of strings, and then dis-
played:
Scanner scanner = new Scanner("Let's pause, and then "+ "
reflect.");
List<String> list = new ArrayList<>();
while(scanner.hasNext()) {
String token = scanner.next();
list.add(token);
}
for(String token : list) {
System.out.println(token);
}
When executed, we get the following output:
Let's
pause,
and
then
reflect.
This simple implementation has several shortcomings. If we needed our contractions to be
identified and possibly split, as demonstrated with the first token, then this implementation
fails to do it. Also, the last word of the sentence was returned with a period attached to it.
Specifying the delimiter
If we are not happy with the default delimiter, there are several methods we can use to
change its behavior. Several of these methods are summarized in the following table Re-
Search WWH ::




Custom Search