The Scanner
Class
Because of the complications of the levels of hierarchy in the Files class, starting in Java 1.5, a new class was introduced, called the Scanner class. It is in the class java.util.Scanner, so to use it, one must have
import java.util.*;
inside the class which is using it. The Scanner class can only be used for text files (i.e., ASCII encoded files) to parse primitive data types (char, int, double, etc.). In general, it will read from the file byte by byte until it finds a whitespace. A whitespace is one of three characters: (1) blank space, (2) tab, or (3) a line feed. We refer to the data read in between whitespaces as tokens. (Note, for example, that three consecutive blank characters are considered to be just one whitespace.) You type in the line feed by hitting the Enter key on your keyboard.
The advantage of using an object of the Scanner class is that one only has to create it, and then one can read in data using the appropriate methods of the class. For example, if we have the data of the type in a file called Sample.txt:
widget 14 3.35
then the following code will read in it:
String name;
int units;
double price;
Scanner scan = new Scanner(new File(“Sample.txt”));
name = scan.next();
units = scan.nextInt();
price = scan.nextDouble();
The method next, reads in character by character as a char until it finds a whitespace. Similarly, nextInt reads in character by character (from the previous whitespace) until if finds a whitespace. (Note that three consecutive characters of whitespace are considered to be just one whitespace.) So for the above, name will have the value of “widget”, units the value of 14, and price the value of 3.35.
There are also methods in the Scanner class which allow you to see what the next input is without having to actually get it. E.g., one has a method hasNextInt which returns true if and only if the next token in the scanner’s input can be interpreted as an int. However, it does not actually get it.
You can also use the Scanner class to read in from the console window. Just as System.out is the reference to putting output to the console window, the System.in is the reference to getting input from the console window. Thus,
Scanner scan = new Scanner(System.in);
int a,b;
System.out.print(“Give the first number: “);
a = scan.nextInt();
System.out.print(“Give the second number: “);
b = scan.nextInt();
System.out.print(“The sum of the two numbers given is “+ (a + b));
Then, when this program executes, at the console window, one would see the following:
Give the first number:
If one type in a 6, followed by the Enter key, the console window would look like:
Give the first number: 6
Give the second number:
Next, if one typed in a 51 and again hit the Enter key, the console window would look like:
Give the first number: 6
Give the second number: 51
The sum of the two numbers given is 57
This is because, when one typed in the first number 6, it was read in by the Scanner class object scan and stored in the variable a. The next time the object scan read in the number 51 and assigned it to b.
Note that if one had written for the last line the following:
System.out.print(“The sum of the two numbers given is “+ a + b);
The output would have been
The sum of the two numbers given is 651
Why is this so?