Java Scanner next(String pattern) method example

HQ ┬╗ Java Tutorial ┬╗ java.util ┬╗ Scanner ┬╗ next(String pattern) method example
Java Scanner next(String pattern) method example 2018-02-20T12:21:55+00:00

java.util.Scanner next(String pattern)

Description :

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

Method Syntax :

public String next(String pattern)

Parameter Input :

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

Method Returns :

This method simply returns the next token.

Compatibility Version :

Requires Java 1.5 and up

Exception :


Discussion :

The Scanner next(String 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.

Java Code Example :

This java example source code demonstrates the use of next(String pattern) method of Scanner class. First we have declared the String to tokenize using the Scanner constructor. A string pattern was in place to list down the tokens consists of numbers only.

Sample Output :

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


Similar Method :

  • N/A

Suggested Reading List :

References :


Contact Info


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.