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 ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.