java.util.Scanner hasNext(Pattern pattern)

Description :

This java tutorial shows how to use the hasNext(Pattern 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 object specified as method argument.

Method Syntax :

public boolean hasNext(Pattern pattern)

Parameter Input :


DataType Parameter Description
Pattern pattern An object 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 object Pattern specified as method argument, otherwise it’s false.

Compatibility Version :

Requires Java 1.5 and up

Exception :


The IllegalStateException will be thrown by hasNext(Pattern pattern) method of Scanner class if the method invocation is done after the scanner is closed.

Discussion :

The Scanner hasNext(Pattern pattern) method is used a flag if we can iterate further on the String tokens. This is a long version of calling the method hasNext(String pattern). Looking at the java api source code, this is the same method as the latter. However the only difference is that instead of String pattern, the method argument is Pattern object.

Java Code Example :

This java example source code demonstrates the use of hasNext(Pattern pattern) method of Scanner class. Basically the code prints only those tokens that consists of alphabet on its character elements.


import java.util.Scanner;
import java.util.regex.Pattern;

 * This is a java example source code that shows the
 * usage of hasNext(Patten pattern)
 * method of Scanner class. This code scans only tokens that consists
 * of characters on the alphabet only
 * Java Tutorial

public class ScannerHasNextPatternDemo {

	public static void main(String[] args) {

		// Initialize Scanner object
		Scanner scan = new Scanner("Vince1;Gandhi;Albert");
		// declare the delimiter to be used by Scanner object

		/*Initialize the String pattern which
		signifies that the String token contains
		characters of the alphabet only*/
		Pattern pattern = Pattern.compile("[A-Za-z]*");

			// check if the token consists of declared pattern

		// closing the scanner stream



Sample Output :

Running the hasNext(Patttern 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 :

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
	at java.util.regex.Pattern.error(Unknown Source)
	at java.util.regex.Pattern.sequence(Unknown Source)
	at java.util.regex.Pattern.expr(Unknown Source)
	at java.util.regex.Pattern.compile(Unknown Source)
	at java.util.regex.Pattern.(Unknown Source)
	at java.util.regex.Pattern.compile(Unknown Source)

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
	at java.util.Scanner.ensureOpen(Unknown Source)
	at java.util.Scanner.hasNext(Unknown Source)

Similar Method :

  • N/A

Suggested Reading List :

References :