1 *java.util.Scanner* *Scanner* A simple text scanner which can parse primitive ty
3 public final class Scanner
4 extends |java.lang.Object|
5 implements |java.util.Iterator|
7 |java.util.Scanner_Description|
8 |java.util.Scanner_Fields|
9 |java.util.Scanner_Constructors|
10 |java.util.Scanner_Methods|
12 ================================================================================
14 *java.util.Scanner_Constructors*
15 |java.util.Scanner(File)|Constructs a new Scanner that produces values scanned
16 |java.util.Scanner(File,String)|Constructs a new Scanner that produces values s
17 |java.util.Scanner(InputStream)|Constructs a new Scanner that produces values s
18 |java.util.Scanner(InputStream,String)|Constructs a new Scanner that produces v
19 |java.util.Scanner(Readable)|Constructs a new Scanner that produces values scan
20 |java.util.Scanner(ReadableByteChannel)|Constructs a new Scanner that produces
21 |java.util.Scanner(ReadableByteChannel,String)|Constructs a new Scanner that pr
22 |java.util.Scanner(String)|Constructs a new Scanner that produces values scanne
24 *java.util.Scanner_Methods*
25 |java.util.Scanner.close()|Closes this scanner.
26 |java.util.Scanner.delimiter()|Returns the Pattern this Scanner is currently u
27 |java.util.Scanner.findInLine(Pattern)|Attempts to find the next occurrence of
28 |java.util.Scanner.findInLine(String)|Attempts to find the next occurrence of a
29 |java.util.Scanner.findWithinHorizon(Pattern,int)|Attempts to find the next occ
30 |java.util.Scanner.findWithinHorizon(String,int)|Attempts to find the next occu
31 |java.util.Scanner.hasNext()|Returns true if this scanner has another token in
32 |java.util.Scanner.hasNext(Pattern)|Returns true if the next complete token mat
33 |java.util.Scanner.hasNext(String)|Returns true if the next token matches the p
34 |java.util.Scanner.hasNextBigDecimal()|Returns true if the next token in this s
35 |java.util.Scanner.hasNextBigInteger()|Returns true if the next token in this s
36 |java.util.Scanner.hasNextBigInteger(int)|Returns true if the next token in thi
37 |java.util.Scanner.hasNextBoolean()|Returns true if the next token in this scan
38 |java.util.Scanner.hasNextByte()|Returns true if the next token in this scanner
39 |java.util.Scanner.hasNextByte(int)|Returns true if the next token in this scan
40 |java.util.Scanner.hasNextDouble()|Returns true if the next token in this scann
41 |java.util.Scanner.hasNextFloat()|Returns true if the next token in this scanne
42 |java.util.Scanner.hasNextInt()|Returns true if the next token in this scanner'
43 |java.util.Scanner.hasNextInt(int)|Returns true if the next token in this scann
44 |java.util.Scanner.hasNextLine()|Returns true if there is another line in the i
45 |java.util.Scanner.hasNextLong()|Returns true if the next token in this scanner
46 |java.util.Scanner.hasNextLong(int)|Returns true if the next token in this scan
47 |java.util.Scanner.hasNextShort()|Returns true if the next token in this scanne
48 |java.util.Scanner.hasNextShort(int)|Returns true if the next token in this sca
49 |java.util.Scanner.ioException()|Returns the IOException last thrown by this S
50 |java.util.Scanner.locale()|Returns this scanner's locale.
51 |java.util.Scanner.match()|Returns the match result of the last scanning operat
52 |java.util.Scanner.next()|Finds and returns the next complete token from this s
53 |java.util.Scanner.next(Pattern)|Returns the next token if it matches the speci
54 |java.util.Scanner.next(String)|Returns the next token if it matches the patter
55 |java.util.Scanner.nextBigDecimal()|Scans the next token of the input as ajava.
56 |java.util.Scanner.nextBigInteger()|Scans the next token of the input as ajava.
57 |java.util.Scanner.nextBigInteger(int)|Scans the next token of the input as aja
58 |java.util.Scanner.nextBoolean()|Scans the next token of the input into a boole
59 |java.util.Scanner.nextByte()|Scans the next token of the input as a byte.
60 |java.util.Scanner.nextByte(int)|Scans the next token of the input as a byte.
61 |java.util.Scanner.nextDouble()|Scans the next token of the input as a double.
62 |java.util.Scanner.nextFloat()|Scans the next token of the input as a float.
63 |java.util.Scanner.nextInt()|Scans the next token of the input as an int.
64 |java.util.Scanner.nextInt(int)|Scans the next token of the input as an int.
65 |java.util.Scanner.nextLine()|Advances this scanner past the current line and r
66 |java.util.Scanner.nextLong()|Scans the next token of the input as a long.
67 |java.util.Scanner.nextLong(int)|Scans the next token of the input as a long.
68 |java.util.Scanner.nextShort()|Scans the next token of the input as a short.
69 |java.util.Scanner.nextShort(int)|Scans the next token of the input as a short.
70 |java.util.Scanner.radix()|Returns this scanner's default radix.
71 |java.util.Scanner.remove()|The remove operation is not supported by this imple
72 |java.util.Scanner.reset()|Resets this scanner.
73 |java.util.Scanner.skip(Pattern)|Skips input that matches the specified pattern
74 |java.util.Scanner.skip(String)|Skips input that matches a pattern constructed
75 |java.util.Scanner.toString()|Returns the string representation of this Scanner
76 |java.util.Scanner.useDelimiter(Pattern)|Sets this scanner's delimiting pattern
77 |java.util.Scanner.useDelimiter(String)|Sets this scanner's delimiting pattern
78 |java.util.Scanner.useLocale(Locale)|Sets this scanner's locale to the specifie
79 |java.util.Scanner.useRadix(int)|Sets this scanner's default radix to the speci
81 *java.util.Scanner_Description*
83 A simple text scanner which can parse primitive types and strings using regular
86 A Scanner breaks its input into tokens using a delimiter pattern, which by
87 default matches whitespace. The resulting tokens may then be converted into
88 values of different types using the various next methods.
90 For example, this code allows a user to read a number from System.in:
92 Scanner sc = new Scanner(System.in); int i = sc.nextInt();
94 As another example, this code allows long types to be assigned from entries in
97 Scanner sc = new Scanner(new File("myNumbers")); while (sc.hasNextLong()) {
98 long aLong = sc.nextLong(); }
100 The scanner can also use delimiters other than whitespace. This example reads
101 several items in from a string:
103 String input = "1 fish 2 fish red fish blue fish"; Scanner s = new
104 Scanner(input).useDelimiter("\\s*fish\\s*"); System.out.println(s.nextInt());
105 System.out.println(s.nextInt()); System.out.println(s.next());
106 System.out.println(s.next()); s.close();
108 prints the following output:
112 The same output can be generated with this code, which uses a regular
113 expression to parse all four tokens at once:
115 String input = "1 fish 2 fish red fish blue fish"; Scanner s = new
116 Scanner(input); s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
117 MatchResult result = s.match(); for (int i=1; i
119 The default whitespace delimiter used by a scanner is as recognized by
120 (|java.lang.Character|) . isWhitespace(|java.lang.Character|) . The
121 (|java.util.Scanner|) method will reset the value of the scanner's delimiter to
122 the default whitespace delimiter regardless of whether it was previously
125 A scanning operation may block waiting for input.
127 The (|java.util.Scanner|) and (|java.util.Scanner|) methods and their
128 primitive-type companion methods (such as (|java.util.Scanner|) and
129 (|java.util.Scanner|) ) first skip any input that matches the delimiter
130 pattern, and then attempt to return the next token. Both hasNext and next
131 methods may block waiting for further input. Whether a hasNext method blocks
132 has no connection to whether or not its associated next method will block.
134 The (|java.util.Scanner|) , (|java.util.Scanner|) , and (|java.util.Scanner|)
135 methods operate independently of the delimiter pattern. These methods will
136 attempt to match the specified pattern with no regard to delimiters in the
137 input and thus can be used in special circumstances where delimiters are not
138 relevant. These methods may block waiting for more input.
140 When a scanner throws an (|java.util.InputMismatchException|) , the scanner
141 will not pass the token that caused the exception, so that it may be retrieved
142 or skipped via some other method.
144 Depending upon the type of delimiting pattern, empty tokens may be returned.
145 For example, the pattern "\\s+" will return no empty tokens since it matches
146 multiple instances of the delimiter. The delimiting pattern "\\s" could return
147 empty tokens since it only passes one space at a time.
149 A scanner can read text from any object which implements the
150 (|java.lang.Readable|) interface. If an invocation of the underlying readable's
151 (|java.lang.Readable|) method throws an (|java.io.IOException|) then the
152 scanner assumes that the end of the input has been reached. The most recent
153 IOException thrown by the underlying readable can be retrieved via the
154 (|java.util.Scanner|) method.
156 When a Scanner is closed, it will close its input source if the source
157 implements the (|java.io.Closeable|) interface.
159 A Scanner is not safe for multithreaded use without external synchronization.
161 Unless otherwise mentioned, passing a null parameter into any method of a
162 Scanner will cause a NullPointerException to be thrown.
164 A scanner will default to interpreting numbers as decimal unless a different
165 radix has been set by using the (|java.util.Scanner|) method. The
166 (|java.util.Scanner|) method will reset the value of the scanner's radix to 10
167 regardless of whether it was previously changed.
171 An instance of this class is capable of scanning numbers in the standard
172 formats as well as in the formats of the scanner's locale. A scanner's initial
173 locale is the value returned by the (|java.util.Locale|) method; it may be
174 changed via the (|java.util.Scanner|) method. The (|java.util.Scanner|) method
175 will reset the value of the scanner's locale to the initial locale regardless
176 of whether it was previously changed.
178 The localized formats are defined in terms of the following parameters, which
179 for a particular locale are taken from that locale's
180 DecimalFormat(|java.text.DecimalFormat|) object, df, and its and
181 DecimalFormatSymbols(|java.text.DecimalFormatSymbols|) object, dfs.
183 LocalGroupSeparator The character used to separate thousands groups, i.e.,dfs.
184 getGroupingSeparator()(|java.text.DecimalFormatSymbols|) LocalDecimalSeparator
185 The character used for the decimal point, i.e.,dfs.
186 getDecimalSeparator()(|java.text.DecimalFormatSymbols|) LocalPositivePrefix The
187 string that appears before a positive number (may be empty), i.e.,df.
188 getPositivePrefix()(|java.text.DecimalFormat|) LocalPositiveSuffix The string
189 that appears after a positive number (may be empty), i.e.,df.
190 getPositiveSuffix()(|java.text.DecimalFormat|) LocalNegativePrefix The string
191 that appears before a negative number (may be empty), i.e.,df.
192 getNegativePrefix()(|java.text.DecimalFormat|) LocalNegativeSuffix The string
193 that appears after a negative number (may be empty), i.e.,df.
194 getNegativeSuffix()(|java.text.DecimalFormat|) LocalNaN The string that
195 represents not-a-number for floating-point values, i.e.,dfs.
196 getNaN()(|java.text.DecimalFormatSymbols|) LocalInfinity The string that
197 represents infinity for floating-point values, i.e.,dfs.
198 getInfinity()(|java.text.DecimalFormatSymbols|)
204 The strings that can be parsed as numbers by an instance of this class are
205 specified in terms of the following regular-expression grammar, where Rmax is
206 the highest digit in the radix being used (for example, Rmax is 9 in base 10).
210 NonASCIIDigit:: = A non-ASCII character c for which
211 Character.isDigit(|java.lang.Character|) (c) returnstrue
215 Non0Digit:: = [1-Rmax] | NonASCIIDigit
219 Digit:: = [0-Rmax] | NonASCIIDigit
225 = ( Non0Digit Digit? Digit?
227 (LocalGroupSeparator Digit Digit Digit )+ )
231 Numeral:: = ( ( Digit+ ) | GroupedNumeral )
235 Integer:: = ( [-+]? ( Numeral ) )
237 | LocalPositivePrefix Numeral LocalPositiveSuffix
239 | LocalNegativePrefix Numeral LocalNegativeSuffix
243 DecimalNumeral:: = Numeral
245 | Numeral LocalDecimalSeparator Digit*
247 | LocalDecimalSeparator Digit+
251 Exponent:: = ( [eE] [+-]? Digit+ )
255 Decimal:: = ( [-+]? DecimalNumeral Exponent? )
257 | LocalPositivePrefix DecimalNumeral LocalPositiveSuffix Exponent?
259 | LocalNegativePrefix DecimalNumeral LocalNegativeSuffix Exponent?
263 HexFloat:: = [-+]? 0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+ ([pP][-+]?[0-9]+)?
267 NonNumber:: = NaN | LocalNan | Infinity | LocalInfinity
271 SignedNonNumber:: = ( [-+]? NonNumber )
273 | LocalPositivePrefix NonNumber LocalPositiveSuffix
275 | LocalNegativePrefix NonNumber LocalNegativeSuffix
287 Whitespace is not significant in the above regular expressions.
291 *java.util.Scanner(File)*
293 public Scanner(java.io.File source)
294 throws |java.io.FileNotFoundException|
296 Constructs a new Scanner that produces values scanned from the specified file.
297 Bytes from the file are converted into characters using the underlying
298 platform's default charset(|java.nio.charset.Charset|) .
300 source - A file to be scanned
302 *java.util.Scanner(File,String)*
306 java.lang.String charsetName)
307 throws |java.io.FileNotFoundException|
309 Constructs a new Scanner that produces values scanned from the specified file.
310 Bytes from the file are converted into characters using the specified charset.
312 source - A file to be scanned
313 charsetName - The encoding type used to convert bytes from the file into characters to be
316 *java.util.Scanner(InputStream)*
318 public Scanner(java.io.InputStream source)
320 Constructs a new Scanner that produces values scanned from the specified input
321 stream. Bytes from the stream are converted into characters using the
322 underlying platform's default charset(|java.nio.charset.Charset|) .
324 source - An input stream to be scanned
326 *java.util.Scanner(InputStream,String)*
329 java.io.InputStream source,
330 java.lang.String charsetName)
332 Constructs a new Scanner that produces values scanned from the specified input
333 stream. Bytes from the stream are converted into characters using the specified
336 source - An input stream to be scanned
337 charsetName - The encoding type used to convert bytes from the stream into characters to be
340 *java.util.Scanner(Readable)*
342 public Scanner(java.lang.Readable source)
344 Constructs a new Scanner that produces values scanned from the specified
347 source - A character source implementing the {@link Readable} interface
349 *java.util.Scanner(ReadableByteChannel)*
351 public Scanner(java.nio.channels.ReadableByteChannel source)
353 Constructs a new Scanner that produces values scanned from the specified
354 channel. Bytes from the source are converted into characters using the
355 underlying platform's default charset(|java.nio.charset.Charset|) .
357 source - A channel to scan
359 *java.util.Scanner(ReadableByteChannel,String)*
362 java.nio.channels.ReadableByteChannel source,
363 java.lang.String charsetName)
365 Constructs a new Scanner that produces values scanned from the specified
366 channel. Bytes from the source are converted into characters using the
369 source - A channel to scan
370 charsetName - The encoding type used to convert bytes from the channel into characters to be
373 *java.util.Scanner(String)*
375 public Scanner(java.lang.String source)
377 Constructs a new Scanner that produces values scanned from the specified
380 source - A string to scan
382 *java.util.Scanner.close()*
388 If this scanner has not yet been closed then if its underlying
389 readable(|java.lang.Readable|) also implements the (|java.io.Closeable|)
390 interface then the readable's close method will be invoked. If this scanner is
391 already closed then invoking this method will have no effect.
393 Attempting to perform search operations after a scanner has been closed will
394 result in an (|java.lang.IllegalStateException|) .
398 *java.util.Scanner.delimiter()*
400 public |java.util.regex.Pattern| delimiter()
402 Returns the Pattern this Scanner is currently using to match delimiters.
406 Returns: this scanner's delimiting pattern.
408 *java.util.Scanner.findInLine(Pattern)*
410 public |java.lang.String| findInLine(java.util.regex.Pattern pattern)
412 Attempts to find the next occurrence of the specified pattern ignoring
413 delimiters. If the pattern is found before the next line separator, the scanner
414 advances past the input that matched and returns the string that matched the
415 pattern. If no such pattern is detected in the input up to the next line
416 separator, then null is returned and the scanner's position is unchanged. This
417 method may block waiting for input that matches the pattern.
419 Since this method continues to search through the input looking for the
420 specified pattern, it may buffer all of the input searching for the desired
421 token if no line separators are present.
424 pattern - the pattern to scan for
426 Returns: the text that matched the specified pattern
428 *java.util.Scanner.findInLine(String)*
430 public |java.lang.String| findInLine(java.lang.String pattern)
432 Attempts to find the next occurrence of a pattern constructed from the
433 specified string, ignoring delimiters.
435 An invocation of this method of the form findInLine(pattern) behaves in exactly
436 the same way as the invocation findInLine(Pattern.compile(pattern)).
439 pattern - a string specifying the pattern to search for
441 Returns: the text that matched the specified pattern
443 *java.util.Scanner.findWithinHorizon(Pattern,int)*
445 public |java.lang.String| findWithinHorizon(
446 java.util.regex.Pattern pattern,
449 Attempts to find the next occurrence of the specified pattern.
451 This method searches through the input up to the specified search horizon,
452 ignoring delimiters. If the pattern is found the scanner advances past the
453 input that matched and returns the string that matched the pattern. If no such
454 pattern is detected then the null is returned and the scanner's position
455 remains unchanged. This method may block waiting for input that matches the
458 A scanner will never search more than horizon code points beyond its current
459 position. Note that a match may be clipped by the horizon; that is, an
460 arbitrary match result may have been different if the horizon had been larger.
461 The scanner treats the horizon as a transparent, non-anchoring bound (see
462 (|java.util.regex.Matcher|) and (|java.util.regex.Matcher|) ).
464 If horizon is 0, then the horizon is ignored and this method continues to
465 search through the input looking for the specified pattern without bound. In
466 this case it may buffer all of the input searching for the pattern.
468 If horizon is negative, then an IllegalArgumentException is thrown.
471 pattern - the pattern to scan for
473 Returns: the text that matched the specified pattern
475 *java.util.Scanner.findWithinHorizon(String,int)*
477 public |java.lang.String| findWithinHorizon(
478 java.lang.String pattern,
481 Attempts to find the next occurrence of a pattern constructed from the
482 specified string, ignoring delimiters.
484 An invocation of this method of the form findWithinHorizon(pattern) behaves in
485 exactly the same way as the invocation
486 findWithinHorizon(Pattern.compile(pattern, horizon)).
489 pattern - a string specifying the pattern to search for
491 Returns: the text that matched the specified pattern
493 *java.util.Scanner.hasNext()*
495 public boolean hasNext()
497 Returns true if this scanner has another token in its input. This method may
498 block while waiting for input to scan. The scanner does not advance past any
503 Returns: true if and only if this scanner has another token
505 *java.util.Scanner.hasNext(Pattern)*
507 public boolean hasNext(java.util.regex.Pattern pattern)
509 Returns true if the next complete token matches the specified pattern. A
510 complete token is prefixed and postfixed by input that matches the delimiter
511 pattern. This method may block while waiting for input. The scanner does not
512 advance past any input.
515 pattern - the pattern to scan for
517 Returns: true if and only if this scanner has another token matching the specified
520 *java.util.Scanner.hasNext(String)*
522 public boolean hasNext(java.lang.String pattern)
524 Returns true if the next token matches the pattern constructed from the
525 specified string. The scanner does not advance past any input.
527 An invocation of this method of the form hasNext(pattern) behaves in exactly
528 the same way as the invocation hasNext(Pattern.compile(pattern)).
531 pattern - a string specifying the pattern to scan
533 Returns: true if and only if this scanner has another token matching the specified
536 *java.util.Scanner.hasNextBigDecimal()*
538 public boolean hasNextBigDecimal()
540 Returns true if the next token in this scanner's input can be interpreted as a
541 BigDecimal using the (|java.util.Scanner|) method. The scanner does not advance
546 Returns: true if and only if this scanner's next token is a valid BigDecimal
548 *java.util.Scanner.hasNextBigInteger()*
550 public boolean hasNextBigInteger()
552 Returns true if the next token in this scanner's input can be interpreted as a
553 BigInteger in the default radix using the (|java.util.Scanner|) method. The
554 scanner does not advance past any input.
558 Returns: true if and only if this scanner's next token is a valid BigInteger
560 *java.util.Scanner.hasNextBigInteger(int)*
562 public boolean hasNextBigInteger(int radix)
564 Returns true if the next token in this scanner's input can be interpreted as a
565 BigInteger in the specified radix using the (|java.util.Scanner|) method. The
566 scanner does not advance past any input.
569 radix - the radix used to interpret the token as an integer
571 Returns: true if and only if this scanner's next token is a valid BigInteger
573 *java.util.Scanner.hasNextBoolean()*
575 public boolean hasNextBoolean()
577 Returns true if the next token in this scanner's input can be interpreted as a
578 boolean value using a case insensitive pattern created from the string
579 "true|false". The scanner does not advance past the input that matched.
583 Returns: true if and only if this scanner's next token is a valid boolean value
585 *java.util.Scanner.hasNextByte()*
587 public boolean hasNextByte()
589 Returns true if the next token in this scanner's input can be interpreted as a
590 byte value in the default radix using the (|java.util.Scanner|) method. The
591 scanner does not advance past any input.
595 Returns: true if and only if this scanner's next token is a valid byte value
597 *java.util.Scanner.hasNextByte(int)*
599 public boolean hasNextByte(int radix)
601 Returns true if the next token in this scanner's input can be interpreted as a
602 byte value in the specified radix using the (|java.util.Scanner|) method. The
603 scanner does not advance past any input.
606 radix - the radix used to interpret the token as a byte value
608 Returns: true if and only if this scanner's next token is a valid byte value
610 *java.util.Scanner.hasNextDouble()*
612 public boolean hasNextDouble()
614 Returns true if the next token in this scanner's input can be interpreted as a
615 double value using the (|java.util.Scanner|) method. The scanner does not
616 advance past any input.
620 Returns: true if and only if this scanner's next token is a valid double value
622 *java.util.Scanner.hasNextFloat()*
624 public boolean hasNextFloat()
626 Returns true if the next token in this scanner's input can be interpreted as a
627 float value using the (|java.util.Scanner|) method. The scanner does not
628 advance past any input.
632 Returns: true if and only if this scanner's next token is a valid float value
634 *java.util.Scanner.hasNextInt()*
636 public boolean hasNextInt()
638 Returns true if the next token in this scanner's input can be interpreted as an
639 int value in the default radix using the (|java.util.Scanner|) method. The
640 scanner does not advance past any input.
644 Returns: true if and only if this scanner's next token is a valid int value
646 *java.util.Scanner.hasNextInt(int)*
648 public boolean hasNextInt(int radix)
650 Returns true if the next token in this scanner's input can be interpreted as an
651 int value in the specified radix using the (|java.util.Scanner|) method. The
652 scanner does not advance past any input.
655 radix - the radix used to interpret the token as an int value
657 Returns: true if and only if this scanner's next token is a valid int value
659 *java.util.Scanner.hasNextLine()*
661 public boolean hasNextLine()
663 Returns true if there is another line in the input of this scanner. This method
664 may block while waiting for input. The scanner does not advance past any input.
668 Returns: true if and only if this scanner has another line of input
670 *java.util.Scanner.hasNextLong()*
672 public boolean hasNextLong()
674 Returns true if the next token in this scanner's input can be interpreted as a
675 long value in the default radix using the (|java.util.Scanner|) method. The
676 scanner does not advance past any input.
680 Returns: true if and only if this scanner's next token is a valid long value
682 *java.util.Scanner.hasNextLong(int)*
684 public boolean hasNextLong(int radix)
686 Returns true if the next token in this scanner's input can be interpreted as a
687 long value in the specified radix using the (|java.util.Scanner|) method. The
688 scanner does not advance past any input.
691 radix - the radix used to interpret the token as a long value
693 Returns: true if and only if this scanner's next token is a valid long value
695 *java.util.Scanner.hasNextShort()*
697 public boolean hasNextShort()
699 Returns true if the next token in this scanner's input can be interpreted as a
700 short value in the default radix using the (|java.util.Scanner|) method. The
701 scanner does not advance past any input.
705 Returns: true if and only if this scanner's next token is a valid short value in the
708 *java.util.Scanner.hasNextShort(int)*
710 public boolean hasNextShort(int radix)
712 Returns true if the next token in this scanner's input can be interpreted as a
713 short value in the specified radix using the (|java.util.Scanner|) method. The
714 scanner does not advance past any input.
717 radix - the radix used to interpret the token as a short value
719 Returns: true if and only if this scanner's next token is a valid short value in the
722 *java.util.Scanner.ioException()*
724 public |java.io.IOException| ioException()
726 Returns the IOException last thrown by this Scanner's underlying Readable. This
727 method returns null if no such exception exists.
731 Returns: the last exception thrown by this scanner's readable
733 *java.util.Scanner.locale()*
735 public |java.util.Locale| locale()
737 Returns this scanner's locale.
739 A scanner's locale affects many elements of its default primitive matching
740 regular expressions; see localized numbers above.
744 Returns: this scanner's locale
746 *java.util.Scanner.match()*
748 public |java.util.regex.MatchResult| match()
750 Returns the match result of the last scanning operation performed by this
751 scanner. This method throws IllegalStateException if no match has been
752 performed, or if the last match was not successful.
754 The various nextmethods of Scanner make a match result available if they
755 complete without throwing an exception. For instance, after an invocation of
756 the (|java.util.Scanner|) method that returned an int, this method returns a
757 MatchResult for the search of the Integer regular expression defined above.
758 Similarly the (|java.util.Scanner|) , (|java.util.Scanner|) , and
759 (|java.util.Scanner|) methods will make a match available if they succeed.
763 Returns: a match result for the last match operation
765 *java.util.Scanner.next()*
767 public |java.lang.String| next()
769 Finds and returns the next complete token from this scanner. A complete token
770 is preceded and followed by input that matches the delimiter pattern. This
771 method may block while waiting for input to scan, even if a previous invocation
772 of (|java.util.Scanner|) returned true.
776 Returns: the next token
778 *java.util.Scanner.next(Pattern)*
780 public |java.lang.String| next(java.util.regex.Pattern pattern)
782 Returns the next token if it matches the specified pattern. This method may
783 block while waiting for input to scan, even if a previous invocation of
784 (|java.util.Scanner|) returned true. If the match is successful, the scanner
785 advances past the input that matched the pattern.
788 pattern - the pattern to scan for
790 Returns: the next token
792 *java.util.Scanner.next(String)*
794 public |java.lang.String| next(java.lang.String pattern)
796 Returns the next token if it matches the pattern constructed from the specified
797 string. If the match is successful, the scanner advances past the input that
800 An invocation of this method of the form next(pattern) behaves in exactly the
801 same way as the invocation next(Pattern.compile(pattern)).
804 pattern - a string specifying the pattern to scan
806 Returns: the next token
808 *java.util.Scanner.nextBigDecimal()*
810 public |java.math.BigDecimal| nextBigDecimal()
812 Scans the next token of the input as a BigDecimal(|java.math.BigDecimal|) .
814 If the next token matches the Decimal regular expression defined above then the
815 token is converted into a BigDecimal value as if by removing all group
816 separators, mapping non-ASCII digits into ASCII digits via the
817 Character.digit(|java.lang.Character|) , and passing the resulting string to
818 the BigDecimal(String)(|java.math.BigDecimal|) constructor.
822 Returns: the BigDecimal scanned from the input
824 *java.util.Scanner.nextBigInteger()*
826 public |java.math.BigInteger| nextBigInteger()
828 Scans the next token of the input as a BigInteger(|java.math.BigInteger|) .
830 An invocation of this method of the form nextBigInteger() behaves in exactly
831 the same way as the invocation nextBigInteger(radix), where radix is the
832 default radix of this scanner.
836 Returns: the BigInteger scanned from the input
838 *java.util.Scanner.nextBigInteger(int)*
840 public |java.math.BigInteger| nextBigInteger(int radix)
842 Scans the next token of the input as a BigInteger(|java.math.BigInteger|) .
844 If the next token matches the Integer regular expression defined above then the
845 token is converted into a BigInteger value as if by removing all group
846 separators, mapping non-ASCII digits into ASCII digits via the
847 Character.digit(|java.lang.Character|) , and passing the resulting string to
848 the BigInteger(String, int)(|java.math.BigInteger|) constructor with the
852 radix - the radix used to interpret the token
854 Returns: the BigInteger scanned from the input
856 *java.util.Scanner.nextBoolean()*
858 public boolean nextBoolean()
860 Scans the next token of the input into a boolean value and returns that value.
861 This method will throw InputMismatchException if the next token cannot be
862 translated into a valid boolean value. If the match is successful, the scanner
863 advances past the input that matched.
867 Returns: the boolean scanned from the input
869 *java.util.Scanner.nextByte()*
871 public byte nextByte()
873 Scans the next token of the input as a byte.
875 An invocation of this method of the form nextByte() behaves in exactly the same
876 way as the invocation nextByte(radix), where radix is the default radix of this
881 Returns: the byte scanned from the input
883 *java.util.Scanner.nextByte(int)*
885 public byte nextByte(int radix)
887 Scans the next token of the input as a byte. This method will throw
888 InputMismatchException if the next token cannot be translated into a valid byte
889 value as described below. If the translation is successful, the scanner
890 advances past the input that matched.
892 If the next token matches the Integer regular expression defined above then the
893 token is converted into a byte value as if by removing all locale specific
894 prefixes, group separators, and locale specific suffixes, then mapping
895 non-ASCII digits into ASCII digits via Character.digit(|java.lang.Character|) ,
896 prepending a negative sign (-) if the locale specific negative prefixes and
897 suffixes were present, and passing the resulting string to
898 Byte.parseByte(|java.lang.Byte|) with the specified radix.
901 radix - the radix used to interpret the token as a byte value
903 Returns: the byte scanned from the input
905 *java.util.Scanner.nextDouble()*
907 public double nextDouble()
909 Scans the next token of the input as a double. This method will throw
910 InputMismatchException if the next token cannot be translated into a valid
911 double value. If the translation is successful, the scanner advances past the
914 If the next token matches the Float regular expression defined above then the
915 token is converted into a double value as if by removing all locale specific
916 prefixes, group separators, and locale specific suffixes, then mapping
917 non-ASCII digits into ASCII digits via Character.digit(|java.lang.Character|) ,
918 prepending a negative sign (-) if the locale specific negative prefixes and
919 suffixes were present, and passing the resulting string to
920 Double.parseDouble(|java.lang.Double|) . If the token matches the localized NaN
921 or infinity strings, then either "Nan" or "Infinity" is passed to
922 Double.parseDouble(|java.lang.Double|) as appropriate.
926 Returns: the double scanned from the input
928 *java.util.Scanner.nextFloat()*
930 public float nextFloat()
932 Scans the next token of the input as a float. This method will throw
933 InputMismatchException if the next token cannot be translated into a valid
934 float value as described below. If the translation is successful, the scanner
935 advances past the input that matched.
937 If the next token matches the Float regular expression defined above then the
938 token is converted into a float value as if by removing all locale specific
939 prefixes, group separators, and locale specific suffixes, then mapping
940 non-ASCII digits into ASCII digits via Character.digit(|java.lang.Character|) ,
941 prepending a negative sign (-) if the locale specific negative prefixes and
942 suffixes were present, and passing the resulting string to
943 Float.parseFloat(|java.lang.Float|) . If the token matches the localized NaN or
944 infinity strings, then either "Nan" or "Infinity" is passed to
945 Float.parseFloat(|java.lang.Float|) as appropriate.
949 Returns: the float scanned from the input
951 *java.util.Scanner.nextInt()*
955 Scans the next token of the input as an int.
957 An invocation of this method of the form nextInt() behaves in exactly the same
958 way as the invocation nextInt(radix), where radix is the default radix of this
963 Returns: the int scanned from the input
965 *java.util.Scanner.nextInt(int)*
967 public int nextInt(int radix)
969 Scans the next token of the input as an int. This method will throw
970 InputMismatchException if the next token cannot be translated into a valid int
971 value as described below. If the translation is successful, the scanner
972 advances past the input that matched.
974 If the next token matches the Integer regular expression defined above then the
975 token is converted into an int value as if by removing all locale specific
976 prefixes, group separators, and locale specific suffixes, then mapping
977 non-ASCII digits into ASCII digits via Character.digit(|java.lang.Character|) ,
978 prepending a negative sign (-) if the locale specific negative prefixes and
979 suffixes were present, and passing the resulting string to
980 Integer.parseInt(|java.lang.Integer|) with the specified radix.
983 radix - the radix used to interpret the token as an int value
985 Returns: the int scanned from the input
987 *java.util.Scanner.nextLine()*
989 public |java.lang.String| nextLine()
991 Advances this scanner past the current line and returns the input that was
994 This method returns the rest of the current line, excluding any line separator
995 at the end. The position is set to the beginning of the next line.
997 Since this method continues to search through the input looking for a line
998 separator, it may buffer all of the input searching for the line to skip if no
999 line separators are present.
1003 Returns: the line that was skipped
1005 *java.util.Scanner.nextLong()*
1007 public long nextLong()
1009 Scans the next token of the input as a long.
1011 An invocation of this method of the form nextLong() behaves in exactly the same
1012 way as the invocation nextLong(radix), where radix is the default radix of this
1017 Returns: the long scanned from the input
1019 *java.util.Scanner.nextLong(int)*
1021 public long nextLong(int radix)
1023 Scans the next token of the input as a long. This method will throw
1024 InputMismatchException if the next token cannot be translated into a valid long
1025 value as described below. If the translation is successful, the scanner
1026 advances past the input that matched.
1028 If the next token matches the Integer regular expression defined above then the
1029 token is converted into a long value as if by removing all locale specific
1030 prefixes, group separators, and locale specific suffixes, then mapping
1031 non-ASCII digits into ASCII digits via Character.digit(|java.lang.Character|) ,
1032 prepending a negative sign (-) if the locale specific negative prefixes and
1033 suffixes were present, and passing the resulting string to
1034 Long.parseLong(|java.lang.Long|) with the specified radix.
1037 radix - the radix used to interpret the token as an int value
1039 Returns: the long scanned from the input
1041 *java.util.Scanner.nextShort()*
1043 public short nextShort()
1045 Scans the next token of the input as a short.
1047 An invocation of this method of the form nextShort() behaves in exactly the
1048 same way as the invocation nextShort(radix), where radix is the default radix
1053 Returns: the short scanned from the input
1055 *java.util.Scanner.nextShort(int)*
1057 public short nextShort(int radix)
1059 Scans the next token of the input as a short. This method will throw
1060 InputMismatchException if the next token cannot be translated into a valid
1061 short value as described below. If the translation is successful, the scanner
1062 advances past the input that matched.
1064 If the next token matches the Integer regular expression defined above then the
1065 token is converted into a short value as if by removing all locale specific
1066 prefixes, group separators, and locale specific suffixes, then mapping
1067 non-ASCII digits into ASCII digits via Character.digit(|java.lang.Character|) ,
1068 prepending a negative sign (-) if the locale specific negative prefixes and
1069 suffixes were present, and passing the resulting string to
1070 Short.parseShort(|java.lang.Short|) with the specified radix.
1073 radix - the radix used to interpret the token as a short value
1075 Returns: the short scanned from the input
1077 *java.util.Scanner.radix()*
1081 Returns this scanner's default radix.
1083 A scanner's radix affects elements of its default number matching regular
1084 expressions; see localized numbers above.
1088 Returns: the default radix of this scanner
1090 *java.util.Scanner.remove()*
1092 public void remove()
1094 The remove operation is not supported by this implementation of Iterator.
1098 *java.util.Scanner.reset()*
1100 public |java.util.Scanner| reset()
1102 Resets this scanner.
1104 Resetting a scanner discards all of its explicit state information which may
1105 have been changed by invocations of (|java.util.Scanner|) ,
1106 (|java.util.Scanner|) , or (|java.util.Scanner|) .
1108 An invocation of this method of the form scanner.reset() behaves in exactly the
1109 same way as the invocation
1113 scanner.useDelimiter("\\p{javaWhitespace}+") .useLocale(Locale.getDefault())
1120 *java.util.Scanner.skip(Pattern)*
1122 public |java.util.Scanner| skip(java.util.regex.Pattern pattern)
1124 Skips input that matches the specified pattern, ignoring delimiters. This
1125 method will skip input if an anchored match of the specified pattern succeeds.
1127 If a match to the specified pattern is not found at the current position, then
1128 no input is skipped and a NoSuchElementException is thrown.
1130 Since this method seeks to match the specified pattern starting at the
1131 scanner's current position, patterns that can match a lot of input (".*", for
1132 example) may cause the scanner to buffer a large amount of input.
1134 Note that it is possible to skip something without risking a
1135 NoSuchElementException by using a pattern that can match nothing, e.g.,
1139 pattern - a string specifying the pattern to skip over
1143 *java.util.Scanner.skip(String)*
1145 public |java.util.Scanner| skip(java.lang.String pattern)
1147 Skips input that matches a pattern constructed from the specified string.
1149 An invocation of this method of the form skip(pattern) behaves in exactly the
1150 same way as the invocation skip(Pattern.compile(pattern)).
1153 pattern - a string specifying the pattern to skip over
1157 *java.util.Scanner.toString()*
1159 public |java.lang.String| toString()
1161 Returns the string representation of this Scanner. The string representation of
1162 a Scanner contains information that may be useful for debugging. The exact
1163 format is unspecified.
1167 Returns: The string representation of this scanner
1169 *java.util.Scanner.useDelimiter(Pattern)*
1171 public |java.util.Scanner| useDelimiter(java.util.regex.Pattern pattern)
1173 Sets this scanner's delimiting pattern to the specified pattern.
1176 pattern - A delimiting pattern
1180 *java.util.Scanner.useDelimiter(String)*
1182 public |java.util.Scanner| useDelimiter(java.lang.String pattern)
1184 Sets this scanner's delimiting pattern to a pattern constructed from the
1187 An invocation of this method of the form useDelimiter(pattern) behaves in
1188 exactly the same way as the invocation useDelimiter(Pattern.compile(pattern)).
1190 Invoking the (|java.util.Scanner|) method will set the scanner's delimiter to
1194 pattern - A string specifying a delimiting pattern
1198 *java.util.Scanner.useLocale(Locale)*
1200 public |java.util.Scanner| useLocale(java.util.Locale locale)
1202 Sets this scanner's locale to the specified locale.
1204 A scanner's locale affects many elements of its default primitive matching
1205 regular expressions; see localized numbers above.
1207 Invoking the (|java.util.Scanner|) method will set the scanner's locale to the
1211 locale - A string specifying the locale to use
1215 *java.util.Scanner.useRadix(int)*
1217 public |java.util.Scanner| useRadix(int radix)
1219 Sets this scanner's default radix to the specified radix.
1221 A scanner's radix affects elements of its default number matching regular
1222 expressions; see localized numbers above.
1224 If the radix is less than Character.MIN_RADIX or greater than
1225 Character.MAX_RADIX, then an IllegalArgumentException is thrown.
1227 Invoking the (|java.util.Scanner|) method will set the scanner's radix to 10.
1230 radix - The radix to use when scanning numbers