Scanner Class

Package: java.util

The Scanner class is designed to read and parse data from an input stream. The input stream can be console input or a file stream connected to a text file.

CrossRef.eps The Scanner class is one of many Java I/O classes that use streams. For more information, see Streams (Overview).

Constructor

Constructor

Explanation

Scanner(File source)

Constructs a Scanner object using a file as the input source.

Scanner(InputStream source)

Constructs a Scanner object using an input stream as the input source.

To read data from a command line console, use the constructor as follows:

static Scanner sc = new Scanner(System.in);

Methods

Method

Explanation

boolean hasNextBoolean()

Returns true if the next value entered by the user is a valid boolean value.

boolean hasNextByte()

Returns true if the next value entered by the user is a valid byte value.

boolean hasNextDouble()

Returns true if the next value entered by the user is a valid double value.

boolean hasNextFloat()

Returns true if the next value entered by the user is a valid float value.

boolean hasNextInt()

Returns true if the next value entered by the user is a valid int value.

boolean hasNextLong()

Returns true if the next value entered by the user is a valid long value.

boolean hasNextShort()

Returns true if the next value entered by the user is a valid short value

boolean nextBoolean()

Reads a boolean value from the user.

byte nextByte()

Reads a byte value from the user.

double nextDouble()

Reads a double value from the user.

float nextFloat()

Reads a float value from the user.

int nextInt()

Reads an int value from the user.

String nextLine()

Reads a String value from the user.

long nextLong()

Reads a long value from the user.

short nextShort()

Reads a short value from the user.

Getting input

To read an input value from the user, you can use one of the next methods of the Scanner class. Each primitive data type has its own next method. So the nextInt method, for example, returns an int value.

Because these methods read a value from an input source and return the value, you most often use them in statements that assign the value to a variable like this:

Scanner sc = new Scanner(System.in);

int x = sc.nextInt();

In this example, the nextInt method causes the program to wait for the user to enter a value in the console window.

To let the user know what kind of input the program expects, you should typically call the System.out.print method before you call a Scanner method to get input, like this:

Scanner sc = new Scanner(System.in);

System.out.println(“Please enter an integer:”);

int x = sc.nextInt();

TechnicalStuff.eps If the input source contains a value that can’t be converted to the correct data type, the Scanner class throws an Input MismatchException exception. Because invalid input data is inevitable, you should always handle this exception, as in this example:

Scanner sc = new Scanner(System.in);

System.out.println(“Please enter an integer:”);

int x;

try

{

x = sc.nextInt();

}

catch (InputMismatchException ex)

{

System.out.println(“That is not an integer!”);

}

tip.eps You can prevent the InputMismatchException from being thrown by first testing for valid input data using one of the has methods. For example:

Scanner sc = new Scanner(System.in);

System.out.println(“Please enter an integer:”);

int x;

if (sc.hasInt())

{

x = sc.nextInt();

}

else

{

x = 0;

}

Here, the variable x is assigned the value 0 (zero) if the user does not enter a valid integer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset