Scanner Class: in Java - Util Scanner
Scanner Class: in Java - Util Scanner
Scanner Class: in Java - Util Scanner
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 be read.
Class Constructors
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.
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 a float 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
we have 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
44 23
2222222222
22222.33 End
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 line, nextLine returns the empty string. nextLine never jumps
over a carriage return to get the next line of input. For example, the following code
fragment
int number = in.nextInt();
and the data shown above, string would contain ≥23≤ and string2 would contain
the empty string.
Here is a program that uses these methods, followed by the output. Look over the
application carefully to be sure you understand how the output was generated.
//**********************************************************************
//**********************************************************************
import java.util.Scanner;
// Declarations
int integer;
long longInteger;
float realNumber;
double doubleReal;
String string1;
String string2;
// Prompts
System.out.println("Enter an integer, a long integer, "
+ "and a string.");
// Read in values
integer = in.nextInt();
longInteger = in.nextLong();
realNumber = in.nextFloat();
doubleReal = in.nextDouble();
string1 = in.nextLine();
string2 = in.next();
Output:
23
24
23.4
Boolean Methods
We said that the Scanner methods that read numeric data throw a
InputMismatchException exception if the next value isnπt what the method
expects. We can avoid that problem using Boolean methods. Here are four useful
Boolean methods that allow us to check to be sure that the next value is what we expect.
Method Returns
boolean hasNextLine() Returns true if the scanner has another line in its
input; false otherwise.
boolean hasNextInt() Returns true if the next token in the scanner can
be interpreted as an int value.
boolean hasNextFloat() Returns true if the next toke in the scanner can be
interpreted as a float value.
Let's write a code fragment that instantiates a scanner and reads and prints an integer
value and a second integer value if there is one.
System.out.println(in.nextInt());
if (in.hasNextInt())
System.out.println(in.nextInt());
There are methods equivalent to these for each of the Java built-in types.
The following application applies the appropriate reading method to the data that is keyed
in.
//************************************************************************
// MixedTypeInput
// This application demonstrates testing before reading to be
//************************************************************************
import java.io.*;
import java.util.Scanner;
double number;
if (in.hasNextInt())
number = (double)in.nextInt();
else if (in.hasNextFloat())
number = (double)in.nextFloat();
else if (in.hasNextDouble())
number = in.nextDouble();
}
else
The application was run four times. The input is shown in red.
55000
55000.0
55E10
What would happen if there were no token in the file in the previous example? Each of
the boolean methods would return false. They return true if and only if the next
token in the scanner can be interpreted as a value of their type. We return to the subject of
reading data from files later in this chapter and show how to use these Scanner
methods to allow us to read multiple values from a line in a file. Except for some trivial
cases, we must combine reading operations with loops to read through all of the data on a
file.
Files
To read from a file rather than the keyboard, you instantiate a Scanner object with a
FileReader object rather than System.in.
inFile.hasNext();
inFile.hasNextLine();
return true if inFile has another token in the file or if there is another line in the file.
What about the methods hasNextInt and so forth that we used to look ahead at the
type of the next input token? These can be used to determine if there are more data
values in the file, provided you know exactly how the files are organized
Be sure to close all files. If you forget to close System.in, no harm is done, but
forgetting to close a file can cause problems.