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 following sequence uses a simple string. The next method retrieves the next token from the input stream. The tokens are isolated from the string, stored into a list of strings, and then displayed:

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, this implementation fails to do it. Also, the last word of the sentence was returned with a period attached to it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset