Scanner Class in Java

Last Updated : 29 Jan 2026

Java provides various ways to read input from the user, and the Scanner class is one of the simplest and most commonly used approaches.

In this chapter, we will learn what the Scanner class is, why it is used, its declaration, constructors, important methods, and examples to understand how it works for reading input and parsing data.

What is Scanner Class in Java?

The Scanner class is used to read input data from different sources such as keyboard input, files, strings, or streams.

It breaks the input into tokens using a delimiter, which is whitespace by default, and allows us to read data in different primitive types like int, long, double, float, byte, boolean, and String.

The Scanner class belongs to the java.util package. It extends the Object class and implements the Iterator<String> and Closeable interfaces.

Scanner is widely used because it provides easy-to-use methods like nextInt(), nextLine(), nextDouble(), and nextBoolean() to parse input efficiently.

Scanner Class Declaration

The Scanner class is defined in the java.util package.

How to Get a Scanner Object?

Reading Input from Keyboard

To read input from the user, we pass System.in to the Scanner constructor.

Reading Input from a String

To parse data from a string, we pass the string to the Scanner constructor.

Constructors of Scanner Class

The Scanner class provides multiple constructors to read input from different sources.

1. Scanner(InputStream source)

This constructor creates a Scanner that reads input from an input stream such as System.in.

Syntax:

Here is the syntax:

2. Scanner(InputStream source, String charsetName)

This constructor creates a Scanner that reads input from an input stream using the specified character encoding.

Syntax:

Here is the syntax:

3. Scanner(String source)

This constructor creates a Scanner that reads input from the specified string.

Syntax:

Here is the syntax:

4. Scanner(File source)

This constructor creates a Scanner that reads input from a file.

Syntax:

Here is the syntax:

5. Scanner(File source, String charsetName)

This constructor creates a Scanner that reads input from a file using the specified character encoding.

Syntax:

Here is the syntax:

6. Scanner(Readable source)

This constructor creates a Scanner that reads input from any object implementing the Readable interface.

Syntax:

Here is the syntax:

7. Scanner(ReadableByteChannel source)

This constructor creates a Scanner that reads input from a readable byte channel.

Syntax:

Here is the syntax:

8. Scanner(ReadableByteChannel source, String charsetName)

This constructor creates a Scanner that reads input from a readable byte channel using the specified character encoding.

Syntax:

Here is the syntax:

9. Scanner(Path source)

This constructor creates a Scanner that reads input from a file path.

Syntax:

Here is the syntax:

10. Scanner(Path source, String charsetName)

This constructor creates a Scanner that reads input from a file path using the specified character encoding.

Syntax:

Here is the syntax:

Methods of Scanner Class

The Scanner class provides many useful methods to read and parse input.

Modifier & TypeMethodDescription
voidclose()It is used to close this scanner.
patterndelimiter()It is used to get the Pattern which the Scanner class is currently using to match delimiters.
Stream<MatchResult>findAll()It is used to find a stream of match results that match the provided pattern string.
StringfindInLine()It is used to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
stringfindWithinHorizon()It is used to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
booleanhasNext()It returns true if this scanner has another token in its input.
booleanhasNextBigDecimal()It is used to check if the next token in this scanner's input can be interpreted as a BigDecimal using the nextBigDecimal() method or not.
booleanhasNextBigInteger()It is used to check if the next token in this scanner's input can be interpreted as a BigDecimal using the nextBigDecimal() method or not.
booleanhasNextBoolean()It is used to check if the next token in this scanner's input can be interpreted as a Boolean using the nextBoolean() method or not.
booleanhasNextByte()It is used to check if the next token in this scanner's input can be interpreted as a Byte using the nextBigDecimal() method or not.
booleanhasNextDouble()It is used to check if the next token in this scanner's input can be interpreted as a BigDecimal using the nextByte() method or not.
booleanhasNextFloat()It is used to check if the next token in this scanner's input can be interpreted as a Float using the nextFloat() method or not.
booleanhasNextInt()It is used to check if the next token in this scanner's input can be interpreted as an int using the nextInt() method or not.
booleanhasNextLine()It is used to check if there is another line in the input of this scanner or not.
booleanhasNextLong()It is used to check if the next token in this scanner's input can be interpreted as a Long using the nextLong() method or not.
booleanhasNextShort()It is used to check if the next token in this scanner's input can be interpreted as a Short using the nextShort() method or not.
IOExceptionioException()It is used to get the IOException last thrown by this Scanner's readable.
Localelocale()It is used to get a Locale of the Scanner class.
MatchResultmatch()It is used to get the match result of the last scanning operation performed by this scanner.
Stringnext()It is used to get the next complete token from the scanner which is in use.
BigDecimalnextBigDecimal()It scans the next token of the input as a BigDecimal.
BigIntegernextBigInteger()It scans the next token of the input as a BigInteger.
booleannextBoolean()It scans the next token of the input into a boolean value and returns that value.
bytenextByte()It scans the next token of the input as a byte.
doublenextDouble()It scans the next token of the input as a double.
floatnextFloat()It scans the next token of the input as a float.
intnextInt()It scans the next token of the input as an Int.
StringnextLine()It is used to get the input string that was skipped by the Scanner object.
longnextLong()It scans the next token of the input as a long.
shortnextShort()It scans the next token of the input as a short.
intradix()It is used to get the default radix of the Scanner use.
voidremove()It is used when this implementation of the Iterator does not support the remove operation.
Scannerreset()It is used to reset the Scanner which is in use.
Scannerskip()It skips input that matches the specified pattern, ignoring delimiters
Stream<String>tokens()It is used to get a stream of delimiter-separated tokens from the Scanner object which is in use.
StringtoString()It is used to get the string representation of a Scanner.
ScanneruseDelimiter()It is used to set the delimiting pattern of the Scanner which is in use to the specified pattern.
ScanneruseLocale()It is used to sets this scanner's locale object to the specified locale.
ScanneruseRadix()It is used to set the default radix of the Scanner which is in use to the specified radix.

Examples of Java Scanner Class

Practice the following examples to understand different uses of the Scanner class.

Example 1: Reading a String from User

The following example demonstrates how to read a string input from the user using the nextLine() method.

Output:

Enter your name: Jack
Name is: Jack

Example 2: Reading Multiple Data Types

The following example demonstrates how to read different data types such as String, int, and double using Scanner.

Output:

Enter your name: Andrew
Enter your age: 23
Enter your salary: 25000
Name: Andrew
Age: 23
Salary: 25000.0

Example 3: Using Scanner with Delimiter

The following example demonstrates how to change the delimiter and tokenize a string using the Scanner class.

Output:

--- Tokenized Output ---
Hello
This is TpointTech
My name is Jack.
Delimiter used: /