Sub topic 11
Custom Delimiters with `Scanner`
Assume you know basic Java syntax, Scanner, loops, and regular expressions at a basic level. This chapter shows how to change the separators that Scanner uses between tokens.
The default delimiter
A Scanner breaks input into tokens. A token is a piece of input between separators. By default, the separator is whitespace: spaces, tabs, and line breaks.
import java.util.Scanner;
public class DefaultDelimiter {
public static void main(String[] args) {
Scanner in = new Scanner("red green blue");
while (in.hasNext()) {
System.out.println(in.next());
}
in.close();
}
}The program prints:
red
green
blueThe methods next(), nextInt(), and similar token methods use the scanner's delimiter. The method nextLine() is different. It reads the rest of the current line and does not use the delimiter to split that line.
Changing the delimiter
Call useDelimiter with a regular expression. A regular expression describes a text pattern.
For example, this scanner treats commas as separators:
import java.util.Scanner;
public class CommaDelimiter {
public static void main(String[] args) {
String data = "red,green,blue";
try (Scanner in = new Scanner(data)) {
in.useDelimiter(",");
while (in.hasNext()) {
System.out.println(in.next());
}
}
}
}The output is:
red
green
blueuseDelimiter changes the scanner object and returns that same object, so you can also write:
Scanner in = new Scanner("10;20;30").useDelimiter(";");Handling spaces around separators
Real input often contains spaces after or before a separator. The delimiter can include optional whitespace with \s*.
In a Java string, the backslash must itself be escaped. Therefore, the regular expression \s*,\s* is written as "\\s*,\\s*".
import java.util.Scanner;
public class CommaAndSpaceDelimiter {
public static void main(String[] args) {
String data = "red, green , blue";
try (Scanner in = new Scanner(data)) {
in.useDelimiter("\\s*,\\s*");
while (in.hasNext()) {
System.out.println("[" + in.next() + "]");
}
}
}
}The output is:
[red]
[green]
[blue]The delimiter consumes whitespace next to each comma. The square brackets make each token's boundaries visible.
| Input format | Delimiter pattern | Meaning |
|---|---|---|
| `red green blue` | default | Separate on whitespace |
| `red,green,blue` | `","` | Separate on commas |
| `red, green , blue` | `"\\s*,\\s*"` | Separate on commas and nearby whitespace |
| `10;20;30` | `";"` | Separate on semicolons |
Delimiters are regular expressions
Some characters have special meanings in regular expressions. A pipe means “or,” so it must be escaped when it is meant as a literal character.
import java.util.Scanner;
public class PipeDelimiter {
public static void main(String[] args) {
String data = "Ada|Grace|Linus";
try (Scanner in = new Scanner(data).useDelimiter("\\|")) {
while (in.hasNext()) {
System.out.println(in.next());
}
}
}
}Use "\\|", not "|". The first backslash belongs to the Java string syntax, and the second makes the pipe literal in the regular expression.
A delimiter applies to token methods, but not to nextLine(). If a scanner uses a comma delimiter and you call nextLine(), the method still returns the remaining characters on the current line, including commas.
Avoid delimiter patterns that can match empty text, such as "\\s*". Such patterns can create confusing token boundaries. Make the separator require a real character, such as a comma or semicolon.
Exercise
What delimiter pattern should you use to read java | python | ruby as three tokens without retaining spaces around the pipe?
Show answer
Use "\\s*\\|\\s*", because the escaped pipe is literal and \\s* consumes optional surrounding whitespace.