java.util.Scanner match()

Description :

This java tutorial shows how to use the match() method of Scanner class of java.util package. This method returns a MatchResult object which corresponds to the result of the last operation by the scanner object.

Method Syntax :

public MatchResult match()

Parameter Input :


Method Returns :

This method simply returns the match result of the last operation performed by the Scanner object.

Compatibility Version :

Requires Java 1.5 and up

Exception :


– The IllegalStateException will be thrown by calling the match() method if no match result is available from the last operation such as calling the method nextInt().

Discussion :

The Scanner match() method is helpful in getting useful information in pattern matching. It will give us access to the methods of return type MatchResult which will help us in debugging or can server as flag if we want to proceed further on our pattern matching.

Java Code Example :

This java example source code demonstrates the use of match() method of Scanner class. We have made use of the return type MatchResult to print which index the pattern match has been found.

Sample Output :

Running the match() method example source code of Scanner class will give you the following output:

Exception Scenario :

Similar Method :

  • N/A

Suggested Reading List :

References :