Unit 2 Lesson 7 - Accessor Methods
Lesson summary and assignments
The Scanner Class
The Scanner class enables you to get input from an input-stream. An input stream is an object which feeds characters, bytes, etc. to your program one character at a time. Examples of input stream objects are the keyboard (System.in), a file, a stream of bytes being downloaded from the internet, etc.
Before you can instantiate a Scanner object, you must import the library which defines the Scanner object like so:
import java.util.Scanner;
To create a Scanner object to get input, you call the Scanner constructor like this:
Scanner input = new Scanner(System.in);
This example begins with the name of the class (Scanner) followed by the name you assign to the object you are instantiating (input). This much of the line declares a variable called "input"" of type scanner. The variable does not yet reference an actual Scanner object. You must first create the actual object and assign the object that you create to the input variable you have declared. So, next there is an equals operator which is the assignment operater, and then the code to actually create the scanner object. Like all objects (except arrays covered later in the course) you create the object by entering the word "new" followed by the name of the class (Scanner). Then you enter opening and closing parenthesis inside of which you enclose any parameters required by the constructor. This constructor requires a stream object. "System.in" is a built stream object to send input from the "Standard input" which in our program is the keyboard.
Once you have a Scanner object, you can use it to get input to use in expressions or to assign to variables. The Scanner class has many methods for retrieving input, but in this course we will use the following methods which you access using the dot-operator:
- nextInt()
- nextDouble()
- nextLine()
- close()
The Scanner gives you input one byte or character at a time until it accomplishes the goal of the method method being called. If you call nextInt(), the program will wait for you to type a line such as "45" followed by the enter key. After you press enter, the method will convert the String "45" into the integer value 45 and return it to the code which called the nextInt() method.
At this point, the input stream "System.in" has fed into the Scanner the characters '4' and '5' from the original line you typed. The next character on that line is an end-of-line (eol) character which resulted from you having pressed the enter key. So, if you then call the nextLine() method, the next character which the System.in input stream feeds into the input Scanner is the waiting end-of-line (eol) character. Since the nextLine() method reads characters until it receives an eol character, the method immediately returns to the code which called the nextLine() method, returning an empty string. In this case the nextLine() method never waits for you to type in a line to send as input. The following code demonstrates this scenario.
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num1 = input.nextInt();
System.out.println("input 1: " + num1);
System.out.println("Enter a String: ");
String message = input.nextLine();
System.out.print("Your message was: " + message);
input.close();
In this case, if you type 45<enter> in response to the first input.nextInt() statement, the output of the program will be:
Enter an integer: 45 input 1: 45 Enter a String: Your message was:
Note that in this case the message string that the program thinks you entered is the empty string. This "bug" is by design. It allows you to request the user input several numbers on the same line such as:
System.out.print("Enter three integers: ");
int num1 = input.nextInt();
int num2 = input.nextInt();
int num3 = input.nextInt();
System.out.println("input 1: " + num1);
System.out.println("input 2: " + num2);
System.out.println("input 3: " + num3);
In the example above, the program will stop and wait for you to type something like 45 76 23<enter> after which the program will give the expect output. However, the next character which the Scanner will read from the System.in input stream is the eol character. You need to follow the code above with an empty nextLine() method call to the input Scanner object in order to clear the input stream of the eol character and get it ready for the next line of input like so:
System.out.print("Enter three integers: ");
int num1 = input.nextInt();
int num2 = input.nextInt();
int num3 = input.nextInt();
System.out.println("input 1: " + num1);
System.out.println("input 2: " + num2);
System.out.println("input 3: " + num3);
input.nextInt(); //Clear the input line
System.out.println("Enter a String: ");
String message = input.nextLine();
System.out.print("Your message was: " + message);
input.close();
By placing this extra input.nextInt() call after reading the last integer of the line, the stream is ready to return the next full line which you type when the nextLine() method is called on the input Scanner object.
Note that when you are finished using the Scanner object, you must call the close() method on the input Scanner. This is because when you instantiate the Scanner object, you use and tie up some system resources which other programs running on computer cannot use until you release those resources by calling the close() method. Also, once you call the close method, you cannot use the Scanner object any more unless you re-intantiate a new Scanner object and assign that newly created Scanner object to your input variable.
Answers
Vocabulary
- accessor method
- gives the value that is currently assigned to an instance variable
- application program interface (API)
- a library of prewritten classes
- library
- a collection of methods or reusable components of code
- return by value
- a copy of the value is given to where the method is called