java.util.Scanner next(Pattern pattern)

Description :

This java tutorial shows how to use the next(Pattern pattern) method of Scanner class of java.util package. This method returns the next token which the Scanner object determined using the Pattern object declared as method argument.

Method Syntax :

public String next(Pattern pattern)

Parameter Input :

 

DataType Parameter Description
String pattern This method argument tells the Scanner on which Pattern object to scan

 

Method Returns :

This method simply returns the next String token.

Compatibility Version :

Requires Java 1.5 and up

Exception :

NoSuchElementException

– The next(Pattern pattern) method of String class will throw NoSuchElementException if there are no more tokens available

IllegalStateException

– The IllegalStateException will be thrown if we invoke the next(Pattern pattern) method after the Scanner object is already closed.

Discussion :

The Scanner next(Pattern pattern) method is usually used when we are interested at specific token patterns like if we want to get integers only. This method really helpful as well in catching invalid tokens.

I will not go deeper into discussing regular expressions thus we have limited the discussion only in basic pattern.

The method next(Pattern pattern) is the same as calling the Scanner next(String pattern). Basically these two methods are the same in terms of functionality, the only difference is the object type of method argument. Maybe you are already wondering why do we have to go into trouble in using a Pattern object as method argument. The purpose of these overriding methods is for the purpose of differences in different system architecture. There are a lot of methods in java api that returns a Pattern object instead of Pattern object. In our own code we might also have our own methods that return Pattern object.

Java Code Example :

This java example source code demonstrates the use of next(Pattern pattern) method of Scanner class.

package com.javatutorialhq.java.tutorial.scanner;

import java.util.Scanner;

/*
 * This is a java example source code that shows how to use
 * next(Pattern pattern) method of Scanner class.
 * Split the string into integer tokens
 */

public class ScannerNextStringPatternDemo {

	public static void main(String[] args) {

		// Initialize Scanner object
		Scanner scan = new Scanner("12 313 55 123");
		// intialize the String pattern
		String pattern = "[0-9]*";
		// Printing the tokenized Strings
		while(scan.hasNext()){
			System.out.println(scan.next(pattern));
		}
		// closing the scanner stream
		scan.close();

	}

}

Sample Output :

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

java scanner next(pattern) method example

java scanner next(pattern) method example

Exception Scenario :

Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at com.teknoscope.java.tutorial.scanner.ScannerNextStringPatternDemo.main(ScannerNextStringPatternDemo.java:25)

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
	at java.util.Scanner.ensureOpen(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at com.teknoscope.java.tutorial.scanner.ScannerNextStringPatternDemo.main(ScannerNextStringPatternDemo.java:28)

Similar Method :

  • N/A

Suggested Reading List :

References :