Name

Scanner

Synopsis

This class is a text scanner or tokenizer. It can read input from any Readable object, and convenience constructors can read text from a specified string, file, byte stream, or byte channel. The constructors for files, byte streams, and byte channels optionally allow you to specify the name of the charset to use for byte-to-character conversions.

After creating a Scanner, you can configure it. useDelimiter( ) specifies a regular expression (as a java.util.regex.Pattern or a String) that represents the token delimiter. The default delimiter is any run of whitespace. useLocale( ) specifies the Locale to use for scanning numbers: this may affect things like the character expected for decimal points and the thousands separator. useRadix( ) specifies the radix, or base, in which numbers should be parsed. Any value between 2 and 36 is allowed. These configuration methods may be called at any time and are not required to be called before scanning begins.

Scanner implements the Iterable<String> interface, and you can use the hasNext( ) and next( ) methods of this interface to break the input into a series of String tokens separated by whitespace or by the delimiter specified with useDelimiter( ). In addition to these Iterable methods, however, Scanner defines a number of next X and hasNext X methods for various numeric types X. nextLine( ) returns the next line of input. Two variants of the next( ) method accept a regular expression as an argument and return the next ...

Get Java in a Nutshell, 5th 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.