In Java, sc.useDelimiter(pattern) changes how the scanner splits input into tokens and returns the Scanner itself so calls can be chained.
You can pass either a regex String (compiled automatically) or a precompiled Patternβfor instance "\\s*,\\s*" to trim spaces around CSV commas.
An instance method of java.util.Scanner, available since Java 5 (JDK 1.5). A Scanner reads from any source (System.in for keyboard, a String, or a file) with identical parsing logic; pass the source to new Scanner(...) after import java.util.Scanner; and call sc.useDelimiter(...).
Syntax
Chain useDelimiter(...) right onto new Scanner(...), or call it mid-stream to change separators.
Java
Scanner sc = new Scanner("Ada, 95 , Bob").useDelimiter("\\s*,\\s*");
String name = sc.next(); // "Ada"
int score = sc.nextInt(); // 95
Call
What it does
sc.useDelimiter(",")
Splits on every single comma (adjacent commas produce an empty token).
sc.useDelimiter("\\s*,\\s*")
Splits on commas and trims surrounding spaces/newlines.
sc.useDelimiter(pattern)
Applies a compiled Pattern and returns this.
Because useDelimiter(",") matches exactly one comma, "a,,c" yields "a", "", and "c". Use ",+" if you want consecutive commas to collapse.
Library declarations
Java
// java.util.Scanner
public Scanner useDelimiter(Pattern pattern)
public Scanner useDelimiter(String pattern)
Parameters
Parameter
Description
pattern
Regex String or compiled Pattern matching separators between tokens.
Return Value
Return Value
Description
Scanner
The same scanner instance, enabling method chaining like sc.useDelimiter(",").useLocale(Locale.US).
Throws PatternSyntaxException
If a String argument is not valid regex syntax.
Throws
Nothing. It does not check whether the scanner is closed.