For sequential processing.
EXAMPLE FOR SYNTAX:
The sourceObject can be:
System.in
String
File
InputStream
After use must be closed by invoking the .close() method.
Closing a Scanner on System.in, will close the entire input stream on console so after that no console input can be received.
EXAMPLE FOR FORMULAS:
The nextLine() excluded, all reader methods try to reach data of the primitive type but will have no \n at the end of the read data sequence. Therefore, in case of console reading, after reading data, always read a nextLine() after it or the next read will produce fake data.
The next() and nextLine() read String.
All reader methods trim the data for white spaces.
Scanner will use some default locale settings when reads data. It is possible to use specific locale settings as well.
In case the addressed source is not ready (e.g. scanning a file or an input stream), use try statement.
Scanner implements AutoClosable interface therefore it is possible to use try-with-resources statement with it.
EXAMPLE FOR USING SCANNER:
EXAMPLE FOR USING SCANNER WITH TRY-WITH-RESOURCES:
EXAMPLE FOR USING LOCALE SETTINGS AND TRY-WITH-RESOURCES: