Java Scanner hasNext(String pattern) method example

HQ ┬╗ Java Tutorial ┬╗ java.util ┬╗ Scanner ┬╗ hasNext(String pattern) method example
Java Scanner hasNext(String pattern) method example 2018-02-20T12:15:41+00:00

java.util.Scanner hasNext(String pattern)

Description :

This java tutorial shows how to use the hasNext(String pattern) method of Scanner class of java.util package. This method returns a boolean data type which corresponds if there is an available token given a String pattern specified as method argument.

Method Syntax :

public boolean hasNext(String pattern)

Parameter Input :

DataType Parameter Description
String pattern A string pattern that Scanner object used in tokenizing

Method Returns :

This method simply returns boolean which corresponds the result of pattern matching. Returns true if the Scanner has token that matches the String pattern specified as method argument.

Compatibility Version :

Requires Java 1.5 and up

Exception :

None

Discussion :

The Scanner hasNext(String pattern) method is used a flag if we can iterate further on the String tokens. This is a shortcut of calling the hasNext(Pattern.compile(String pattern)) which yields the same result.

Java Code Example :

This java example source code demonstrates the use of hasNext(String pattern) method of Scanner class. Basically we just check if the pattern satisfies the next token.

Sample Output :

Running the hasNext(String pattern) method example source code of Scanner class will give you the following output

java scanner hasnext(pattern) method example

java scanner hasnext(pattern) method example

Exception Scenario :

N/A

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