bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java Scanner useDelimiter() Method

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java Scanner useDelimiter() Method?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

// ___ a scanner object Scanner myObj = new Scanner("Item 1,Item 2,Item 3");
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The useDelimiter() method changes the delimiter used by the scanner.
Definition and Usage
Java Scanner useDelimiter() Method

❮ Scanner Methods

Read comma separated items

// Create a scanner object Scanner myObj = new Scanner("Item 1,Item 2,Item 3");
// Change delimiter myObj.useDelimiter(",");
// Read the contents of the scanner
while (myObj.hasNext()) {
 System.out.println(myObj.next());
}

Definition and Usage

The useDelimiter() method changes the delimiter used by the scanner. A delimiter is the sequence of characters which separates tokens in the data being scanned. It is described by a regular expression given by a string or a Pattern object.

Learn more about regular expressions in our Java RegEx tutorial .

What is a token?

A token is a sequence of characters separated from other tokens by delimiters. The default delimiter is a block of whitespace characters but it can be changed with this method.

One of the following

public Scanner useDelimiter(Pattern
pattern )
public Scanner useDelimiter(String
pattern )

Parameter Values

ParameterDescription
patternRequired. A string or Pattern object. A regular expression defining which sequences of characters are considered delimiters.

Technical Details

Returns:A reference to the Scanner object that this method belongs to, which allows for chaining configuration methods. An example of chaining is myObj.useDelimiter(",").setRadix(16); .

❮ Scanner Methods

Previous

Java Scanner reset() Method

Next

Java Scanner useLocale() Method