The Scanner Class
The Scanner class is a class used for "scanning" primitive types and
Strings. It can be used to get input from an InputStream, to parse through
a String of text or to read from a file.
Parsing Through a String
The constructor that we will be using from the Scanner class is:
The methods that we will be using from the Scanner class are:
We will now write a basic program to demonstrate how to use the Scanner
class to parse through a String for even numbers.
Output from this program:
Setting the Delimiter
When you create an instance of the Scanner class, the default
delimiter is whitespace. The Scanner class provides methods
to set the delimiter.
We will now write a program that demonstrates how to set the delimiter
of the Scanner class using the useDelimiter(String) method.
Output from this program:
Reading User Input
The constructor that we will be using from the Scanner class is:
The methods that we will be using from the Scanner class are:
We will now write a basic program to demonstrate how to read from
System.in using the Scanner class.
Output from this program:
Reading From a File
The constructor that we will be using from the Scanner class is:
The methods that we will be using from the Scanner class are:
We will now write a basic program to demonstrate how to read from a File
using the Scanner class.
test.txt:
Output from this program:
This message was edited 13 times. Last update was at 02/03/2008 07:08:29
|