Scanner class
Scanner class
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.
If the file ≥myFile≤ is not found, a FileNotFoundException is thrown. This is a checked 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.
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
import java.io.*;
import java.util.Scanner;
class AddTwoNumbers
{
public static void main(String args[])
{
int a, b, c;
System.out.println("Enter two integers to calculate the sum ");
Scanner in = new Scanner(System.in);
a = in.nextInt();
b = in.nextInt();
c = a + b;
System.out.println("Sum of entered integers = "+c);
}
}
import java.io.*;
import java.util.Scanner;
public class MixedTypeInput
{
public static void main(String[] args)
{
double number;
Scanner in = new Scanner(System.in);
System.out.println("Enter your gross income: ");
if (in.hasNextInt())
{
number = (double)in.nextInt();
System.out.println("You entered " + number);
}
else if (in.hasNextFloat())
{
number = (double)in.nextFloat();
System.out.println("You entered " + number);
}
else if (in.hasNextDouble())
{
number = in.nextDouble();
System.out.println("You entered " + number);
}
else
System.out.println("Token not an integer or a real value.");
}
}