Java Scanner match() method example

HQ ┬╗ Java Tutorial ┬╗ java.util ┬╗ Scanner ┬╗ match() method example
Java Scanner match() method example 2018-02-20T12:16:32+00:00

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 :

DataType Parameter Description
N/A N/A N/A

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 :

IllegalStateException

– 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 :

 

Contact Info

Singapore

Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close