The Scanner class is a class in java.util, which allows the user to read values of various types. There are far more methods in class Scanner than you will need in this course. We only cover a small useful subset, ones that allow us to read in numeric values from either the keyboard or file without having to convert them from strings and determine if there are more values to beread.
There are two constructors that are particularly useful: one takes an InputStream object as a parameter and the other takes a FileReader object as a parameter.
Scanner in = new Scanner(System.in); // System.in is an InputStream
Scanner inFile = new Scanner(new FileReader("myFile"));
If the file ≥myFile≤ is not found, a FileNotFoundException is thrown. This is achecked exception, so it must be caught or forwarded by putting the phrase ≥throws FileNotFoundException≤ on the header of the method in which the instantiation occurs and the header of any method that calls the method in which the instantiation occurs.
Numeric and String Methods
Method | Returns |
int nextInt() | Returns the next token as an int. If the next token is not an integer,InputMismatchException is thrown. |
long nextLong() | Returns the next token as a long. If the next token is not an integer, InputMismatchException is thrown. |
float nextFloat() | Returns the next token as a float. If the next token is not a float or is out of range, InputMismatchException is thrown. |
double nextDouble() | Returns the next token as a long. If the next token is not afloat or is out of range, InputMismatchException is thrown. |
String next() | Finds and returns the next complete token from this scanner and returns it as a string; a token is usually ended by whitespace such as a blank or line break. If not token exists, NoSuchElementException is thrown. |
String nextLine() | Returns the rest of the current line, excluding any line separator at the end. |void close() | Closes the scanner. |
The Scanner looks for tokens in the input. A token is a series of characters that ends with what Java calls whitespace. A whitespace character can be a blank, a tab character, a carriage return, or the end of the file. Thus, if we read a line that has a series of numbers separated by blanks, the scanner will take each number as a separate token. Although wehave only shown four numeric methods, each numeric data type has a corresponding method that reads values of that type.
The numeric values may all be on one line with blanks between each value or may be on separate lines. Whitespace characters (blanks or carriage returns) act as separators. The next method returns the next input value as a string, regardless of what is keyed. For example,given the following code segment and data
int number = in.nextInt();
float real = in.nextFloat();
long number2 = in.nextLong();
double real2 = in.nextDouble();
String string = in.next();
44 would be stored in number; 23.0 would be stored in real; 2222222222 would be stored in number2; 22222.33 would be stored in real2; and ≥End≤ would bestored in string. Notice that method nextFloat reads an integer and stores it as a float value. This is legal because an integer value can be stored exactly as a real value; there is no ambiguity. If we had keyed a decimal point after the 44, the system would have thrown a InputMismatchException (a checked exception) regardless of whether or not a non-zero value followed the decimal point. Anarbitrary real number cannot be stored exactly as an integer; it is ambiguous. Remember that anything ambiguous is illegal.
nextLine reads the rest of the line and returns it as a string. The carriage return is consumed but is not appended to the string. The numeric reads do not consume the whitespace, so if a nextLine is issued at after a numeric read and the numeric value is at the end of the...