Java : Scanner with Examples
Scanner (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Scanner methods.
Summary
A simple text scanner which can parse primitive types and strings using regular expressions.
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)
Constructs a new Scanner that produces values scanned from the specified file.
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)
Constructs a new Scanner that produces values scanned from the specified file.
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)
Constructs a new Scanner that produces values scanned from the specified file.
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)
Constructs a new Scanner that produces values scanned from the specified input stream.
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)
Constructs a new Scanner that produces values scanned from the specified input stream.
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)
Constructs a new Scanner that produces values scanned from the specified input stream.
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)
Constructs a new Scanner that produces values scanned from the specified 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)
Constructs a new Scanner that produces values scanned from the specified string.
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)
Constructs a new Scanner that produces values scanned from the specified channel.
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)
Constructs a new Scanner that produces values scanned from the specified channel.
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)
Constructs a new Scanner that produces values scanned from the specified channel.
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)
Constructs a new Scanner that produces values scanned from the specified file.
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)
Constructs a new Scanner that produces values scanned from the specified file.
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)
Constructs a new Scanner that produces values scanned from the specified file.
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 ()
Closes this scanner.
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 ()
Returns the Pattern this Scanner is currently using to match delimiters.
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)
Returns a stream of match results that match the provided pattern string.
This method is equivalent to findAll(Pattern.compile(patString)).
Stream<MatchResult> findAll (Pattern pattern)
Returns a stream of match results from this scanner.
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)
Attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
This method is equivalent to findInLine(Pattern.compile(pattern)).
String findInLine (Pattern pattern)
Attempts to find the next occurrence of the specified pattern ignoring delimiters.
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)
Attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
This method is equivalent to findWithinHorizon(Pattern.compile(pattern), horizon).
String findWithinHorizon (Pattern pattern, int horizon)
Attempts to find the next occurrence of the specified pattern.
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 ()
Returns true if this scanner has another token in its input.
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)
Returns true if the next token matches the pattern constructed from the specified string.
This method is equivalent to hasNext(Pattern.compile(pattern)).
boolean hasNext (Pattern pattern)
Returns true if the next complete token matches the specified 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 ()
Returns true if the next token in this scanner's input can be interpreted as a BigDecimal using the nextBigDecimal() method.
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 ()
Returns true if the next token in this scanner's input can be interpreted as a BigInteger in the default radix using the nextBigInteger() method.
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)
Returns true if the next token in this scanner's input can be interpreted as a BigInteger in the specified radix using the nextBigInteger() method.
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 ()
Returns true if the next token in this scanner's input can be interpreted as a boolean value using a case insensitive pattern created from the string "true|false".
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 ()
Returns true if the next token in this scanner's input can be interpreted as a byte value in the default radix using the nextByte() method.
This method is equivalent except a type to hasNextInt().
boolean hasNextByte (int radix)
Returns true if the next token in this scanner's input can be interpreted as a byte value in the specified radix using the nextByte() method.
This method is equivalent except a type to hasNextInt(int radix).
boolean hasNextDouble ()
Returns true if the next token in this scanner's input can be interpreted as a double value using the nextDouble() method.
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 ()
Returns true if the next token in this scanner's input can be interpreted as a float value using the nextFloat() method.
This method is equivalent except a type to hasNextDouble().
boolean hasNextInt ()
Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method.
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)
Returns true if the next token in this scanner's input can be interpreted as an int value in the specified radix using the nextInt() method.
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 ()
Returns true if there is another line in the input of this scanner.
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 ()
Returns true if the next token in this scanner's input can be interpreted as a long value in the default radix using the nextLong() method.
This method is equivalent except a type to hasNextInt().
boolean hasNextLong (int radix)
Returns true if the next token in this scanner's input can be interpreted as a long value in the specified radix using the nextLong() method.
This method is equivalent except a type to hasNextInt(int radix).
boolean hasNextShort ()
Returns true if the next token in this scanner's input can be interpreted as a short value in the default radix using the nextShort() method.
This method is equivalent except a type to hasNextInt().
boolean hasNextShort (int radix)
Returns true if the next token in this scanner's input can be interpreted as a short value in the specified radix using the nextShort() method.
This method is equivalent except a type to hasNextInt(int radix).
IOException ioException ()
Returns the IOException last thrown by this Scanner's underlying Readable.
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 ()
Returns this scanner's 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 ()
Returns the match result of the last scanning operation performed by this scanner.
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 ()
Finds and returns the next complete token from this scanner.
Please see hasNext().
String next (String pattern)
Returns the next token if it matches the pattern constructed from the specified string.
This method is equivalent to next(Pattern.compile(pattern)).
String next (Pattern pattern)
Returns the next token if it matches the specified 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 ()
Scans the next token of the input as a BigDecimal.
Please see hasNextBigDecimal().
BigInteger nextBigInteger ()
Scans the next token of the input as a BigInteger.
Please see hasNextBigInteger().
BigInteger nextBigInteger (int radix)
Scans the next token of the input as a BigInteger.
Please see hasNextBigInteger(int radix).
boolean nextBoolean ()
Scans the next token of the input into a boolean value and returns that value.
Please see hasNextBoolean().
byte nextByte ()
Scans the next token of the input as a byte.
This method is equivalent except a type to nextInt().
byte nextByte (int radix)
Scans the next token of the input as a byte.
This method is equivalent except a type to nextInt(int radix).
double nextDouble ()
Scans the next token of the input as a double.
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 ()
Scans the next token of the input as a float.
This method is equivalent except a type to nextDouble().
int nextInt ()
Scans the next token of the input as an int.
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)
Scans the next token of the input as an int.
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 ()
Advances this scanner past the current line and returns the input that was skipped.
Please see hasNextLine().
long nextLong ()
Scans the next token of the input as a long.
This method is equivalent except a type to nextInt().
long nextLong (int radix)
Scans the next token of the input as a long.
This method is equivalent except a type to nextInt(int radix).
short nextShort ()
Scans the next token of the input as a short.
This method is equivalent except a type to nextInt().
short nextShort (int radix)
Scans the next token of the input as a short.
This method is equivalent except a type to nextInt(int radix).
int radix ()
Returns this scanner's default 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 ()
The remove operation is not supported by this implementation of Iterator.
try (final var sc = new Scanner("abcd")) {
try {
sc.remove();
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
}
Scanner reset ()
Resets this scanner.
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)
Skips input that matches a pattern constructed from the specified string.
This method is equivalent to skip(Pattern.compile(pattern)).
Scanner skip (Pattern pattern)
Skips input that matches the specified pattern, ignoring delimiters.
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 ()
Returns a stream of delimiter-separated tokens from this scanner.
final var source = """
abcd XYZ
123
""";
try (final var stream = new Scanner(source).tokens()) {
System.out.println(stream.toList()); // [abcd, XYZ, 123]
}
String toString ()
Returns the string representation of this Scanner.
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)
Sets this scanner's delimiting pattern to a pattern constructed from the specified String.
Please see delimiter().
Scanner useDelimiter (Pattern pattern)
Sets this scanner's delimiting pattern to the specified 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)
Sets this scanner's locale to the specified locale.
Please see locale().
Scanner useRadix (int radix)
Sets this scanner's default radix to the specified radix.
Please see radix().
Methods declared in Iterator
Related posts
- API Examples