Java Reference
In-Depth Information
As I mentioned earlier, regular expressions appear in other areas of the Java API, but the exam
objectives only require knowledge of the Pattern and Matcher classes and the String.split
method. Now that we have discussed regular expressions, we can explore the Scanner class.
The Scanner Class
The Scanner class is a text scanner that can parse primitive data type and strings into
tokens. The delimiter for the tokens is either whitespace or a regular expression. The
source of the text can be from a String , File , or InputStream object. You can also assign a
Locale to a Scanner object.
A Scanner is constructed by passing in the source of the data. Here are some of the con-
structors in Scanner :
public Scanner(File source) throws FileNotFoundException
public Scanner(InputStream source)
public Scanner(String source)
By default, the delimiter for parsing the text is whitespace. To assign a regular expres-
sion as the delimiter, invoke one of the useDelimiter methods of Scanner :
public Scanner useDelimiter(Pattern pattern)
public Scanner useDelimiter(String pattern)
The Scanner class defi nes a collection of “next” and “hasNext” methods for parsing
tokens, including a version for strings and each of the primitive types. For example, nextInt()
returns the next int in the input, and hasNextInt() returns true if the next token is an
int . Similarly, nextDouble() and hasNextDouble() are used to read in doubles. The next()
method of the Scanner class reads in the next token as a String , no matter its data type.
Let's look at an example. The following code parses a String into tokens using
whitespace as the delimiter. Study the code and see if you can determine its output:
String source = “abc de fgh 123 ijk”;
Scanner scan = new Scanner(source);
while(scan.hasNext()) {
if(scan.hasNextInt()) {
int x = scan.nextInt();
System.out.println(“int = “ + x);
} else {
String token = scan.next();
System.out.println(token);
}
}
The source String has four spaces that split the string into fi ve tokens. Notice the
fourth token is parsed as an int . The output is
abc
de
Search WWH ::




Custom Search