The Scanner class
A class is a template for an object. A class contains methods for performing particular tasks. From one class we can create many object instances.
The Scanner class belongs to the Java library of classes. It contains methods for reading many different types of data, from different types of input.
System.out
You have already seen System.out in action when we print to the console. This is the default stream for output.
This statement causes the words “Print this’ to be printed to the console in Eclipse.
System.out.println(“Print this”);
What you cannot see, however, are the special line ending characters which this method places at the end of the line. When subsequent lines are printed using println(), they each begin on a new line.
Here’s a different way to print to the console:
System.out.print(“Print this”);
Notice that we are now using the print() method. This method does not include special line ending characters. Each subsequent line of characters that is printed using print() will begin immediately where the last line of characters left off.
and the output...
System.in
The following code example uses Scanner’s nextInt() method to read an int input from System.in (default input from console). The input is assigned to the int variable called aNumber.
Scanner myScanner = new Scanner(System.in);
int aNumber = myScanner.nextInt();
Scanner can be told to read different types of data - or tokens. By default, Scanner uses whitespace characters as delimiters of tokens (the characters that separate tokens).
In the case of the nextInt() function, Scanner is using whitespace to delimit tokens, and return the token as an int. (seen in the figure below).
Other Scanner functions of note include:
next() - Return the next token, of any type.
next(String pattern) - the next token is delimited by the pattern supplied as a parameter.
nextLine() - reads the current line and returns it.
and the output...
In the figures above, the user is prompted to enter information. Methods of the scanner class are used to read the input typed into the console, and assign it to variables. The variable values are then printed to the console.
You can read the entire Scanner class specification here:
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
Java I/O and Import statements
When we wish to use Scanner:
At the top of the code file we import a reference to the Scanner class by using the line:
import java.util.Scanner;
This line says that we intend to use the class called Scanner from the package called java.util.
In Java, all input and output (I/O) is performed by importing methods from the java.io or java.util packages.
Any source of input or destination of output is called a stream: keyboard, screen, network connection, a file, etc., are all streams.
There are three predefined Streams already open and ready to use in every program:
- System.in
- System.out
- System.err
Object Instantiation
To use the Scanner class, we must first declare an instance of Scanner and give it a name. Similar to declaring a variable in that we declare a type and an identifier. In this case, the type is Scanner.
As well as being declared, an object also needs to be instantiated. The use of the new keyword, instantiates the object called myScanner from the Scanner class.
Scanner myScanner = new Scanner(System.in);
In the next statement, we declare an int variable and assign it the value of the next int that is returned myScanner’s nextInt() function.
int aNumber = myScanner.nextInt();
You will notice that when we instantiated a Scanner object, we have also included System.in, inside of the parenthesis (round brackets). This is an example of passing data to a method. Often, a method needs data passed to it so that it can perform its task.
System.in is the input stream from the keyboard, via the Java console, so, we are telling the Scanner object to use this as its source.

