Java : Scanner with Examples
Scanner (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Scanner methods.
Summary
final var sc = new Scanner(System.in);
while (sc.hasNext()) {
final var next = sc.next();
if ("quit".equals(next)) {
System.out.println("Quit!");
break;
}
System.out.println("next : " + next);
}
// Console
------------
Input
↓
abcd <Enter>
Output
↓
next : abcd
------------
Input
↓
123 XYZ <Enter>
Output
↓
next : 123
next : XYZ
------------
Input
↓
quit <Enter>
Output
↓
Quit!
Note : java - Close a Scanner linked to System.in - Stack Overflow
Constructors
Scanner (File source)
final var input = """
abcd
123
""";
final var source = Path.of("R:", "java-work", "aaa.txt");
System.out.println(source); // R:\java-work\aaa.txt
Files.writeString(source, input);
try (final var sc = new Scanner(source.toFile())) {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // abcd
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
Scanner (File source, String charsetName)
final var input = """
○△×
123
""";
final var charsetName = "Shift_JIS";
final var source = Path.of("R:", "java-work", "aaa.txt");
System.out.println(source); // R:\java-work\aaa.txt
Files.writeString(source, input, Charset.forName(charsetName));
try (final var sc = new Scanner(source.toFile(), charsetName)) {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // ○△×
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
Scanner (File source, Charset charset)
final var input = """
○△×
123
""";
final var charset = Charset.forName("Shift_JIS");
final var source = Path.of("R:", "java-work", "aaa.txt");
System.out.println(source); // R:\java-work\aaa.txt
Files.writeString(source, input, charset);
try (final var sc = new Scanner(source.toFile(), charset)) {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // ○△×
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
Scanner (InputStream source)
final var input = """
abcd
123
""";
final var source = new ByteArrayInputStream(input.getBytes());
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // abcd
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
Scanner (InputStream source, String charsetName)
final var input = """
○△×
123
""";
final var charsetName = "Shift_JIS";
final var source = new ByteArrayInputStream(input.getBytes(charsetName));
try (final var sc = new Scanner(source, charsetName)) {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // ○△×
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
Scanner (InputStream source, Charset charset)
final var input = """
○△×
123
""";
final var charset = Charset.forName("Shift_JIS");
final var source = new ByteArrayInputStream(input.getBytes(charset));
try (final var sc = new Scanner(source, charset)) {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // ○△×
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
Scanner (Readable source)
final var input = """
abcd
123
""";
final var source = Path.of("R:", "java-work", "aaa.txt");
System.out.println(source); // R:\java-work\aaa.txt
Files.writeString(source, input);
try (final var sc = new Scanner(Files.newBufferedReader(source))) {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // abcd
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
Scanner (String source)
final var source = """
abcd
123
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // abcd
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
Scanner (ReadableByteChannel source)
final var input = """
abcd
123
""";
final var source = Path.of("R:", "java-work", "aaa.txt");
System.out.println(source); // R:\java-work\aaa.txt
Files.writeString(source, input);
try (final var sc = new Scanner(
Files.newByteChannel(source, StandardOpenOption.READ))) {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // abcd
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
Scanner (ReadableByteChannel source, String charsetName)
final var input = """
○△×
123
""";
final var charsetName = "Shift_JIS";
final var source = Path.of("R:", "java-work", "aaa.txt");
System.out.println(source); // R:\java-work\aaa.txt
Files.writeString(source, input, Charset.forName(charsetName));
try (final var sc = new Scanner(
Files.newByteChannel(source, StandardOpenOption.READ), charsetName)) {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // ○△×
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
Scanner (ReadableByteChannel source, Charset charset)
final var input = """
○△×
123
""";
final var charset = Charset.forName("Shift_JIS");
final var source = Path.of("R:", "java-work", "aaa.txt");
System.out.println(source); // R:\java-work\aaa.txt
Files.writeString(source, input, charset);
try (final var sc = new Scanner(
Files.newByteChannel(source, StandardOpenOption.READ), charset)) {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // ○△×
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
Scanner (Path source)
final var input = """
abcd
123
""";
final var source = Path.of("R:", "java-work", "aaa.txt");
System.out.println(source); // R:\java-work\aaa.txt
Files.writeString(source, input);
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // abcd
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
Scanner (Path source, String charsetName)
final var input = """
○△×
123
""";
final var charsetName = "Shift_JIS";
final var source = Path.of("R:", "java-work", "aaa.txt");
System.out.println(source); // R:\java-work\aaa.txt
Files.writeString(source, input, Charset.forName(charsetName));
try (final var sc = new Scanner(source, charsetName)) {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // ○△×
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
Scanner (Path source, Charset charset)
final var input = """
○△×
123
""";
final var charset = Charset.forName("Shift_JIS");
final var source = Path.of("R:", "java-work", "aaa.txt");
System.out.println(source); // R:\java-work\aaa.txt
Files.writeString(source, input, charset);
try (final var sc = new Scanner(source, charset)) {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // ○△×
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
Methods
void close ()
final var input = """
abcd
123
""";
final var source = Path.of("R:", "java-work", "aaa.txt");
System.out.println(source); // R:\java-work\aaa.txt
Files.writeString(source, input);
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // abcd
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
// An example without a try-with-resources statement.
final var input = """
abcd
123
""";
final var source = Path.of("R:", "java-work", "aaa.txt");
System.out.println(source); // R:\java-work\aaa.txt
Files.writeString(source, input);
final var sc = new Scanner(source);
try {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // abcd
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
} finally {
sc.close();
}
Pattern delimiter ()
final var source = """
abcd XYZ
123
""";
try (final var sc = new Scanner(source)) {
final var delimiter = sc.delimiter();
System.out.println(delimiter); // \p{javaWhitespace}+
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // abcd
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // XYZ
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
final var source = "abcd=XYZ=123";
try (final var sc = new Scanner(source)) {
sc.useDelimiter("=");
final var delimiter = sc.delimiter();
System.out.println(delimiter); // =
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // abcd
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // XYZ
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
Stream<MatchResult> findAll (String patString)
This method is equivalent to findAll(Pattern.compile(patString)).
Stream<MatchResult> findAll (Pattern pattern)
final var source = "abcd 123 XYZ 456";
final var pattern = Pattern.compile("[a-zA-Z]+");
try (final var stream = new Scanner(source).findAll(pattern)) {
final var ret = stream.map(MatchResult::group).toList();
System.out.println(ret); // [abcd, XYZ]
}
final var source = "abcd 123 XYZ 456";
final var pattern = Pattern.compile("\\d+");
try (final var stream = new Scanner(source).findAll(pattern)) {
final var ret = stream.map(MatchResult::group).toList();
System.out.println(ret); // [123, 456]
}
String findInLine (String pattern)
This method is equivalent to findInLine(Pattern.compile(pattern)).
String findInLine (Pattern pattern)
final var source = """
abcdefg
123
""";
try (final var sc = new Scanner(source)) {
final var ret = sc.findInLine(Pattern.compile("cde"));
System.out.println(ret); // cde
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // fg
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
final var source = """
abcd
123
""";
try (final var sc = new Scanner(source)) {
final var ret = sc.findInLine(Pattern.compile("XYZ"));
System.out.println(ret); // null
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // abcd
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
String findWithinHorizon (String pattern, int horizon)
This method is equivalent to findWithinHorizon(Pattern.compile(pattern), horizon).
String findWithinHorizon (Pattern pattern, int horizon)
final var source = """
abcd
XYZ
""";
try (final var sc = new Scanner(source)) {
final var ret = sc.findWithinHorizon(Pattern.compile("bc"), 2);
System.out.println(ret); // null
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // abcd
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // XYZ
}
try (final var sc = new Scanner(source)) {
final var ret = sc.findWithinHorizon(Pattern.compile("bc"), 3);
System.out.println(ret); // bc
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // d
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // XYZ
}
boolean hasNext ()
final var source = """
abcd efg
XYZ
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // abcd
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // efg
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // XYZ
System.out.println(sc.hasNext()); // false
try {
final var next = sc.next();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// Result
// ↓
//NoSuchElementException!
}
boolean hasNext (String pattern)
This method is equivalent to hasNext(Pattern.compile(pattern)).
boolean hasNext (Pattern pattern)
final var source = "abcd XYZ";
try (final var sc = new Scanner(source)) {
final var lower = Pattern.compile("[a-z]+");
final var upper = Pattern.compile("[A-Z]+");
System.out.println(sc.hasNext(lower)); // true
System.out.println(sc.next(lower)); // abcd
System.out.println(sc.hasNext(lower)); // false
System.out.println(sc.hasNext(upper)); // true
System.out.println(sc.next(upper)); // XYZ
System.out.println(sc.hasNext(upper)); // false
}
boolean hasNextBigDecimal ()
final var source = """
0.12345678901234567890123456789
-9999.0
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNextBigDecimal()); // true
System.out.println(sc.nextBigDecimal()); // 0.12345678901234567890123456789
System.out.println(sc.hasNextBigDecimal()); // true
System.out.println(sc.nextBigDecimal()); // -9999.0
System.out.println(sc.hasNextBigDecimal()); // false
}
boolean hasNextBigInteger ()
final var source = """
123456789012345678901234567890
-9999
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNextBigInteger()); // true
System.out.println(sc.nextBigInteger()); // 123456789012345678901234567890
System.out.println(sc.hasNextBigInteger()); // true
System.out.println(sc.nextBigInteger()); // -9999
System.out.println(sc.hasNextBigInteger()); // false
}
boolean hasNextBigInteger (int radix)
Please see also : hasNextBigInteger()
final var source = """
1234
ff
1000
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNextBigInteger()); // true
System.out.println(sc.nextBigInteger()); // 1234
System.out.println(sc.hasNextBigInteger(16)); // true
System.out.println(sc.nextBigInteger(16)); // 255
System.out.println(sc.hasNextBigInteger(2)); // true
System.out.println(sc.nextBigInteger(2)); // 8
}
boolean hasNextBoolean ()
final var source = """
true
false
TRUE
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNextBoolean()); // true
System.out.println(sc.nextBoolean()); // true
System.out.println(sc.hasNextBoolean()); // true
System.out.println(sc.nextBoolean()); // false
System.out.println(sc.hasNextBoolean()); // true
System.out.println(sc.nextBoolean()); // true
System.out.println(sc.hasNextBoolean()); // false
}
boolean hasNextByte ()
This method is equivalent except a type to hasNextInt().
boolean hasNextByte (int radix)
This method is equivalent except a type to hasNextInt(int radix).
boolean hasNextDouble ()
final var source = """
1.234
9.99E-3
-Infinity
XYZ
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNextDouble()); // true
System.out.println(sc.nextDouble()); // 1.234
System.out.println(sc.hasNextDouble()); // true
System.out.println(sc.nextDouble()); // 0.00999
System.out.println(sc.hasNextDouble()); // true
System.out.println(sc.nextDouble()); // -Infinity
System.out.println(sc.hasNextDouble()); // false
try {
final var next = sc.nextDouble();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// Result
// ↓
//NoSuchElementException!
}
System.out.println(Double.MAX_VALUE); // 1.7976931348623157E308
final var source = """
1.0E308
1.0E309
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNextDouble()); // true
System.out.println(sc.nextDouble()); // 1.0E308
System.out.println(sc.hasNextDouble()); // true
System.out.println(sc.nextDouble()); // Infinity
}
boolean hasNextFloat ()
This method is equivalent except a type to hasNextDouble().
boolean hasNextInt ()
final var source = """
1234
-999
XYZ
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 1234
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // -999
System.out.println(sc.hasNextInt()); // false
try {
final var next = sc.nextInt();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// Result
// ↓
//NoSuchElementException!
}
System.out.println(Integer.MAX_VALUE); // 2147483647
final var source = """
2147483646
2147483647
2147483648
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 2147483646
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 2147483647
System.out.println(sc.hasNextInt()); // false
}
boolean hasNextInt (int radix)
Please see also : hasNextInt()
final var source = """
1234
ff
1000
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 1234
System.out.println(sc.hasNextInt(16)); // true
System.out.println(sc.nextInt(16)); // 255
System.out.println(sc.hasNextInt(2)); // true
System.out.println(sc.nextInt(2)); // 8
}
boolean hasNextLine ()
final var source = """
abcd efg
XYZ
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNextLine()); // true
System.out.println(sc.nextLine()); // abcd efg
System.out.println(sc.hasNextLine()); // true
System.out.println(sc.nextLine()); // XYZ
System.out.println(sc.hasNextLine()); // false
try {
final var next = sc.nextLine();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// Result
// ↓
//NoSuchElementException!
}
boolean hasNextLong ()
This method is equivalent except a type to hasNextInt().
boolean hasNextLong (int radix)
This method is equivalent except a type to hasNextInt(int radix).
boolean hasNextShort ()
This method is equivalent except a type to hasNextInt().
boolean hasNextShort (int radix)
This method is equivalent except a type to hasNextInt(int radix).
IOException ioException ()
try (final var sc = new Scanner("abcd")) {
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // abcd
final var ret = sc.ioException();
System.out.println(ret); // null
}
final var source = new StringReader("abcd");
try (final var sc = new Scanner(source)) {
// Intentionally close the source object to make an error.
source.close();
System.out.println(sc.hasNext()); // false
final var ret = sc.ioException();
System.out.println(ret); // java.io.IOException: Stream closed
}
Locale locale ()
System.out.println(Locale.getDefault(Locale.Category.FORMAT)); // en_US
try (final var sc = new Scanner("abcd")) {
System.out.println(sc.locale()); // en_US
sc.useLocale(Locale.JAPAN);
System.out.println(sc.locale()); // ja_JP
}
MatchResult match ()
try (final var sc = new Scanner("abcdefg")) {
System.out.println(sc.findInLine(Pattern.compile("cde"))); // cde
final var ret = sc.match();
System.out.println(ret.group()); // cde
System.out.println(ret.start()); // 2
System.out.println(ret.end()); // 5
}
String next ()
Please see hasNext().
String next (String pattern)
This method is equivalent to next(Pattern.compile(pattern)).
String next (Pattern pattern)
final var source = "abcd XYZ";
try (final var sc = new Scanner(source)) {
final var lower = Pattern.compile("[a-z]+");
final var upper = Pattern.compile("[A-Z]+");
System.out.println(sc.hasNext(lower)); // true
System.out.println(sc.next(lower)); // abcd
System.out.println(sc.hasNext(lower)); // false
System.out.println(sc.hasNext(upper)); // true
System.out.println(sc.next(upper)); // XYZ
System.out.println(sc.hasNext(upper)); // false
}
BigDecimal nextBigDecimal ()
Please see hasNextBigDecimal().
BigInteger nextBigInteger ()
Please see hasNextBigInteger().
BigInteger nextBigInteger (int radix)
Please see hasNextBigInteger(int radix).
boolean nextBoolean ()
Please see hasNextBoolean().
byte nextByte ()
This method is equivalent except a type to nextInt().
byte nextByte (int radix)
This method is equivalent except a type to nextInt(int radix).
double nextDouble ()
final var source = """
1.234
9.99E-3
-Infinity
XYZ
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNextDouble()); // true
System.out.println(sc.nextDouble()); // 1.234
System.out.println(sc.hasNextDouble()); // true
System.out.println(sc.nextDouble()); // 0.00999
System.out.println(sc.hasNextDouble()); // true
System.out.println(sc.nextDouble()); // -Infinity
System.out.println(sc.hasNextDouble()); // false
try {
final var next = sc.nextDouble();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// Result
// ↓
//NoSuchElementException!
}
System.out.println(Double.MAX_VALUE); // 1.7976931348623157E308
final var source = """
1.0E308
1.0E309
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNextDouble()); // true
System.out.println(sc.nextDouble()); // 1.0E308
System.out.println(sc.hasNextDouble()); // true
System.out.println(sc.nextDouble()); // Infinity
}
float nextFloat ()
This method is equivalent except a type to nextDouble().
int nextInt ()
final var source = """
1234
-999
XYZ
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 1234
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // -999
System.out.println(sc.hasNextInt()); // false
try {
final var next = sc.nextInt();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// Result
// ↓
//NoSuchElementException!
}
System.out.println(Integer.MAX_VALUE); // 2147483647
final var source = """
2147483646
2147483647
2147483648
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 2147483646
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 2147483647
System.out.println(sc.hasNextInt()); // false
}
int nextInt (int radix)
Please see also : nextInt()
final var source = """
1234
ff
1000
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 1234
System.out.println(sc.hasNextInt(16)); // true
System.out.println(sc.nextInt(16)); // 255
System.out.println(sc.hasNextInt(2)); // true
System.out.println(sc.nextInt(2)); // 8
}
String nextLine ()
Please see hasNextLine().
long nextLong ()
This method is equivalent except a type to nextInt().
long nextLong (int radix)
This method is equivalent except a type to nextInt(int radix).
short nextShort ()
This method is equivalent except a type to nextInt().
short nextShort (int radix)
This method is equivalent except a type to nextInt(int radix).
int radix ()
final var source = """
1234
ff
""";
try (final var sc = new Scanner(source)) {
System.out.println(sc.radix()); // 10
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 1234
System.out.println(sc.hasNextInt()); // false
sc.useRadix(16);
System.out.println(sc.radix()); // 16
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 255
}
void remove ()
try (final var sc = new Scanner("abcd")) {
try {
sc.remove();
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
}
Scanner reset ()
try (final var sc = new Scanner("abcd")) {
System.out.println(sc.delimiter()); // \p{javaWhitespace}+
System.out.println(sc.locale()); // en_US
System.out.println(sc.radix()); // 10
sc.useDelimiter("=").useLocale(Locale.JAPAN).useRadix(16);
System.out.println(sc.delimiter()); // =
System.out.println(sc.locale()); // ja_JP
System.out.println(sc.radix()); // 16
sc.reset();
System.out.println(sc.delimiter()); // \p{javaWhitespace}+
System.out.println(sc.locale()); // en_US
System.out.println(sc.radix()); // 10
}
Scanner skip (String pattern)
This method is equivalent to skip(Pattern.compile(pattern)).
Scanner skip (Pattern pattern)
final var source = "abcd 123 XYZ 456";
final var pattern = Pattern.compile("\\p{javaWhitespace}*\\p{Alpha}+");
try (final var sc = new Scanner(source)) {
System.out.println(sc.hasNextInt()); // false
sc.skip(pattern);
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
System.out.println(sc.hasNextInt()); // false
sc.skip(pattern);
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.next()); // 456
}
Stream<String> tokens ()
final var source = """
abcd XYZ
123
""";
try (final var stream = new Scanner(source).tokens()) {
System.out.println(stream.toList()); // [abcd, XYZ, 123]
}
String toString ()
try (final var sc = new Scanner("abcd")) {
final var str = sc.toString();
System.out.println(str);
}
// Result
// ↓
//java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false]
// [need input=false][source closed=false][skipped=false][group separator=\x{2c}]
// [decimal separator=\x{2e}][positive prefix=][negative prefix=\Q-\E]
// [positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E]
Scanner useDelimiter (String pattern)
Please see delimiter().
Scanner useDelimiter (Pattern pattern)
final var source = "abcd=XYZ=123";
try (final var sc = new Scanner(source)) {
sc.useDelimiter(Pattern.compile("="));
final var delimiter = sc.delimiter();
System.out.println(delimiter); // =
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // abcd
System.out.println(sc.hasNext()); // true
System.out.println(sc.next()); // XYZ
System.out.println(sc.hasNextInt()); // true
System.out.println(sc.nextInt()); // 123
}
Scanner useLocale (Locale locale)
Please see locale().
Scanner useRadix (int radix)
Please see radix().
Methods declared in Iterator
Related posts
- API Examples