Java Scanner tokens() Method

The tokens() method of Java Scanner class is used to get a stream of delimiter-separated tokens from the Scanner object which are in using. This method might block waiting for more input.

Syntax

Following is the declaration of tokens() method:

Parameter

This method does not accept any parameter.

Returns

The tokens() method returns a sequential stream of token strings.

Exceptions

IllegalStateException- It will thrown this Exception if the innvocation is done after Scanner is closed.

Compatibility Version

Java 1.9 and above

Example 1

Output:

JavaTpoint
Abhishek
Male
22

Example 2

Output:

1
2
red
blue

Next TopicJava-locale