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.skip(Pattern)|Skips input that matches the specified pattern
73 |java.util.Scanner.skip(String)|Skips input that matches a pattern constructed
74 |java.util.Scanner.toString()|Returns the string representation of this Scanner
75 |java.util.Scanner.useDelimiter(Pattern)|Sets this scanner's delimiting pattern
76 |java.util.Scanner.useDelimiter(String)|Sets this scanner's delimiting pattern
77 |java.util.Scanner.useLocale(Locale)|Sets this scanner's locale to the specifie
78 |java.util.Scanner.useRadix(int)|Sets this scanner's default radix to the speci
80 *java.util.Scanner_Description*
82 A simple text scanner which can parse primitive types and strings using regular
85 A Scanner breaks its input into tokens using a delimiter pattern, which by
86 default matches whitespace. The resulting tokens may then be converted into
87 values of different types using the various next methods.
89 For example, this code allows a user to read a number from System.in:
91 Scanner sc = new Scanner(System.in); int i = sc.nextInt();
93 As another example, this code allows long types to be assigned from entries in
96 Scanner sc = new Scanner(new File("myNumbers")); while (sc.hasNextLong()) {
97 long aLong = sc.nextLong(); }
99 The scanner can also use delimiters other than whitespace. This example reads
100 several items in from a string:
102 String input = "1 fish 2 fish red fish blue fish"; Scanner s = new
103 Scanner(input).useDelimiter("\\s*fish\\s*"); System.out.println(s.nextInt());
104 System.out.println(s.nextInt()); System.out.println(s.next());
105 System.out.println(s.next()); s.close();
107 prints the following output:
111 The same output can be generated with this code, which uses a regular
112 expression to parse all four tokens at once:
114 String input = "1 fish 2 fish red fish blue fish"; Scanner s = new
115 Scanner(input); s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
116 MatchResult result = s.match(); for (int i=1; i
118 The default whitespace delimiter used by a scanner is as recognized by
119 (|java.lang.Character|) . isWhitespace(|java.lang.Character|) .
121 A scanning operation may block waiting for input.
123 The (|java.util.Scanner|) and (|java.util.Scanner|) methods and their
124 primitive-type companion methods (such as (|java.util.Scanner|) and
125 (|java.util.Scanner|) ) first skip any input that matches the delimiter
126 pattern, and then attempt to return the next token. Both hasNext and next
127 methods may block waiting for further input. Whether a hasNext method blocks
128 has no connection to whether or not its associated next method will block.
130 The (|java.util.Scanner|) , (|java.util.Scanner|) , and (|java.util.Scanner|)
131 methods operate independently of the delimiter pattern. These methods will
132 attempt to match the specified pattern with no regard to delimiters in the
133 input and thus can be used in special circumstances where delimiters are not
134 relevant. These methods may block waiting for more input.
136 When a scanner throws an (|java.util.InputMismatchException|) , the scanner
137 will not pass the token that caused the exception, so that it may be retrieved
138 or skipped via some other method.
140 Depending upon the type of delimiting pattern, empty tokens may be returned.
141 For example, the pattern "\\s+" will return no empty tokens since it matches
142 multiple instances of the delimiter. The delimiting pattern "\\s" could return
143 empty tokens since it only passes one space at a time.
145 A scanner can read text from any object which implements the
146 (|java.lang.Readable|) interface. If an invocation of the underlying readable's
147 (|java.lang.Readable|) method throws an (|java.io.IOException|) then the
148 scanner assumes that the end of the input has been reached. The most recent
149 IOException thrown by the underlying readable can be retrieved via the
150 (|java.util.Scanner|) method.
152 When a Scanner is closed, it will close its input source if the source
153 implements the (|java.io.Closeable|) interface.
155 A Scanner is not safe for multithreaded use without external synchronization.
157 Unless otherwise mentioned, passing a null parameter into any method of a
158 Scanner will cause a NullPointerException to be thrown.
160 A scanner will default to interpreting numbers as decimal unless a different
161 radix has been set by using the (|java.util.Scanner|) method.
165 An instance of this class is capable of scanning numbers in the standard
166 formats as well as in the formats of the scanner's locale. A scanner's initial
167 locale is the value returned by the (|java.util.Locale|) method; it may be
168 changed via the (|java.util.Scanner|) method.
170 The localized formats are defined in terms of the following parameters, which
171 for a particular locale are taken from that locale's
172 DecimalFormat(|java.text.DecimalFormat|) object, df, and its and
173 DecimalFormatSymbols(|java.text.DecimalFormatSymbols|) object, dfs.
175 LocalGroupSeparator The character used to separate thousands groups, i.e.,dfs.
176 getGroupingSeparator()(|java.text.DecimalFormatSymbols|) LocalDecimalSeparator
177 The character used for the decimal point, i.e.,dfs.
178 getDecimalSeparator()(|java.text.DecimalFormatSymbols|) LocalPositivePrefix The
179 string that appears before a positive number (may be empty), i.e.,df.
180 getPositivePrefix()(|java.text.DecimalFormat|) LocalPositiveSuffix The string
181 that appears after a positive number (may be empty), i.e.,df.
182 getPositiveSuffix()(|java.text.DecimalFormat|) LocalNegativePrefix The string
183 that appears before a negative number (may be empty), i.e.,df.
184 getNegativePrefix()(|java.text.DecimalFormat|) LocalNegativeSuffix The string
185 that appears after a negative number (may be empty), i.e.,df.
186 getNegativeSuffix()(|java.text.DecimalFormat|) LocalNaN The string that
187 represents not-a-number for floating-point values, i.e.,dfs.
188 getInfinity()(|java.text.DecimalFormatSymbols|) LocalInfinity The string that
189 represents infinity for floating-point values, i.e.,dfs.
190 getInfinity()(|java.text.DecimalFormatSymbols|)
196 The strings that can be parsed as numbers by an instance of this class are
197 specified in terms of the following regular-expression grammar, where Rmax is
198 the highest digit in the radix being used (for example, Rmax is 9 in base 10).
202 NonASCIIDigit:: = A non-ASCII character c for which
203 Character.isDigit(|java.lang.Character|) (c) returnstrue
207 Non0Digit:: = [1-Rmax] | NonASCIIDigit
211 Digit:: = [0-Rmax] | NonASCIIDigit
217 = ( Non0Digit Digit? Digit?
219 (LocalGroupSeparator Digit Digit Digit )+ )
223 Numeral:: = ( ( Digit+ ) | GroupedNumeral )
227 Integer:: = ( [-+]? ( Numeral ) )
229 | LocalPositivePrefix Numeral LocalPositiveSuffix
231 | LocalNegativePrefix Numeral LocalNegativeSuffix
235 DecimalNumeral:: = Numeral
237 | Numeral LocalDecimalSeparator Digit*
239 | LocalDecimalSeparator Digit+
243 Exponent:: = ( [eE] [+-]? Digit+ )
247 Decimal:: = ( [-+]? DecimalNumeral Exponent? )
249 | LocalPositivePrefix DecimalNumeral LocalPositiveSuffix Exponent?
251 | LocalNegativePrefix DecimalNumeral LocalNegativeSuffix Exponent?
255 HexFloat:: = [-+]? 0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+ ([pP][-+]?[0-9]+)?
259 NonNumber:: = NaN | LocalNan | Infinity | LocalInfinity
263 SignedNonNumber:: = ( [-+]? NonNumber )
265 | LocalPositivePrefix NonNumber LocalPositiveSuffix
267 | LocalNegativePrefix NonNumber LocalNegativeSuffix
279 Whitespace is not significant in the above regular expressions.
282 *java.util.Scanner(File)*
284 public Scanner(java.io.File source)
285 throws |java.io.FileNotFoundException|
287 Constructs a new Scanner that produces values scanned from the specified file.
288 Bytes from the file are converted into characters using the underlying
289 platform's default charset(|java.nio.charset.Charset|) .
291 source - A file to be scanned
293 *java.util.Scanner(File,String)*
297 java.lang.String charsetName)
298 throws |java.io.FileNotFoundException|
300 Constructs a new Scanner that produces values scanned from the specified file.
301 Bytes from the file are converted into characters using the specified charset.
303 source - A file to be scanned
304 charsetName - The encoding type used to convert bytes from the file into characters to be
307 *java.util.Scanner(InputStream)*
309 public Scanner(java.io.InputStream source)
311 Constructs a new Scanner that produces values scanned from the specified input
312 stream. Bytes from the stream are converted into characters using the
313 underlying platform's default charset(|java.nio.charset.Charset|) .
315 source - An input stream to be scanned
317 *java.util.Scanner(InputStream,String)*
320 java.io.InputStream source,
321 java.lang.String charsetName)
323 Constructs a new Scanner that produces values scanned from the specified input
324 stream. Bytes from the stream are converted into characters using the specified
327 source - An input stream to be scanned
328 charsetName - The encoding type used to convert bytes from the stream into characters to be
331 *java.util.Scanner(Readable)*
333 public Scanner(java.lang.Readable source)
335 Constructs a new Scanner that produces values scanned from the specified
338 source - A character source implementing the {@link Readable} interface
340 *java.util.Scanner(ReadableByteChannel)*
342 public Scanner(java.nio.channels.ReadableByteChannel source)
344 Constructs a new Scanner that produces values scanned from the specified
345 channel. Bytes from the source are converted into characters using the
346 underlying platform's default charset(|java.nio.charset.Charset|) .
348 source - A channel to scan
350 *java.util.Scanner(ReadableByteChannel,String)*
353 java.nio.channels.ReadableByteChannel source,
354 java.lang.String charsetName)
356 Constructs a new Scanner that produces values scanned from the specified
357 channel. Bytes from the source are converted into characters using the
360 source - A channel to scan
361 charsetName - The encoding type used to convert bytes from the channel into characters to be
364 *java.util.Scanner(String)*
366 public Scanner(java.lang.String source)
368 Constructs a new Scanner that produces values scanned from the specified
371 source - A string to scan
373 *java.util.Scanner.close()*
379 If this scanner has not yet been closed then if its underlying
380 readable(|java.lang.Readable|) also implements the (|java.io.Closeable|)
381 interface then the readable's close method will be invoked. If this scanner is
382 already closed then invoking this method will have no effect.
384 Attempting to perform search operations after a scanner has been closed will
385 result in an (|java.lang.IllegalStateException|) .
388 *java.util.Scanner.delimiter()*
390 public |java.util.regex.Pattern| delimiter()
392 Returns the Pattern this Scanner is currently using to match delimiters.
395 Returns: this scanner's delimiting pattern.
396 *java.util.Scanner.findInLine(Pattern)*
398 public |java.lang.String| findInLine(java.util.regex.Pattern pattern)
400 Attempts to find the next occurrence of the specified pattern ignoring
401 delimiters. If the pattern is found before the next line separator, the scanner
402 advances past the input that matched and returns the string that matched the
403 pattern. If no such pattern is detected in the input up to the next line
404 separator, then null is returned and the scanner's position is unchanged. This
405 method may block waiting for input that matches the pattern.
407 Since this method continues to search through the input looking for the
408 specified pattern, it may buffer all of the input searching for the desired
409 token if no line separators are present.
411 pattern - the pattern to scan for
413 Returns: the text that matched the specified pattern
414 *java.util.Scanner.findInLine(String)*
416 public |java.lang.String| findInLine(java.lang.String pattern)
418 Attempts to find the next occurrence of a pattern constructed from the
419 specified string, ignoring delimiters.
421 An invocation of this method of the form findInLine(pattern) behaves in exactly
422 the same way as the invocation findInLine(Pattern.compile(pattern)).
424 pattern - a string specifying the pattern to search for
426 Returns: the text that matched the specified pattern
427 *java.util.Scanner.findWithinHorizon(Pattern,int)*
429 public |java.lang.String| findWithinHorizon(
430 java.util.regex.Pattern pattern,
433 Attempts to find the next occurrence of the specified pattern.
435 This method searches through the input up to the specified search horizon,
436 ignoring delimiters. If the pattern is found the scanner advances past the
437 input that matched and returns the string that matched the pattern. If no such
438 pattern is detected then the null is returned and the scanner's position
439 remains unchanged. This method may block waiting for input that matches the
442 A scanner will never search more than horizon code points beyond its current
443 position. Note that a match may be clipped by the horizon; that is, an
444 arbitrary match result may have been different if the horizon had been larger.
445 The scanner treats the horizon as a transparent, non-anchoring bound (see
446 (|java.util.regex.Matcher|) and (|java.util.regex.Matcher|) ).
448 If horizon is 0, then the horizon is ignored and this method continues to
449 search through the input looking for the specified pattern without bound. In
450 this case it may buffer all of the input searching for the pattern.
452 If horizon is negative, then an IllegalArgumentException is thrown.
454 pattern - the pattern to scan for
456 Returns: the text that matched the specified pattern
457 *java.util.Scanner.findWithinHorizon(String,int)*
459 public |java.lang.String| findWithinHorizon(
460 java.lang.String pattern,
463 Attempts to find the next occurrence of a pattern constructed from the
464 specified string, ignoring delimiters.
466 An invocation of this method of the form findWithinHorizon(pattern) behaves in
467 exactly the same way as the invocation
468 findWithinHorizon(Pattern.compile(pattern, horizon)).
470 pattern - a string specifying the pattern to search for
472 Returns: the text that matched the specified pattern
473 *java.util.Scanner.hasNext()*
475 public boolean hasNext()
477 Returns true if this scanner has another token in its input. This method may
478 block while waiting for input to scan. The scanner does not advance past any
482 Returns: true if and only if this scanner has another token
483 *java.util.Scanner.hasNext(Pattern)*
485 public boolean hasNext(java.util.regex.Pattern pattern)
487 Returns true if the next complete token matches the specified pattern. A
488 complete token is prefixed and postfixed by input that matches the delimiter
489 pattern. This method may block while waiting for input. The scanner does not
490 advance past any input.
492 pattern - the pattern to scan for
494 Returns: true if and only if this scanner has another token matching the specified
496 *java.util.Scanner.hasNext(String)*
498 public boolean hasNext(java.lang.String pattern)
500 Returns true if the next token matches the pattern constructed from the
501 specified string. The scanner does not advance past any input.
503 An invocation of this method of the form hasNext(pattern) behaves in exactly
504 the same way as the invocation hasNext(Pattern.compile(pattern)).
506 pattern - a string specifying the pattern to scan
508 Returns: true if and only if this scanner has another token matching the specified
510 *java.util.Scanner.hasNextBigDecimal()*
512 public boolean hasNextBigDecimal()
514 Returns true if the next token in this scanner's input can be interpreted as a
515 BigDecimal using the (|java.util.Scanner|) method. The scanner does not advance
519 Returns: true if and only if this scanner's next token is a valid BigDecimal
520 *java.util.Scanner.hasNextBigInteger()*
522 public boolean hasNextBigInteger()
524 Returns true if the next token in this scanner's input can be interpreted as a
525 BigInteger in the default radix using the (|java.util.Scanner|) method. The
526 scanner does not advance past any input.
529 Returns: true if and only if this scanner's next token is a valid BigInteger
530 *java.util.Scanner.hasNextBigInteger(int)*
532 public boolean hasNextBigInteger(int radix)
534 Returns true if the next token in this scanner's input can be interpreted as a
535 BigInteger in the specified radix using the (|java.util.Scanner|) method. The
536 scanner does not advance past any input.
538 radix - the radix used to interpret the token as an integer
540 Returns: true if and only if this scanner's next token is a valid BigInteger
541 *java.util.Scanner.hasNextBoolean()*
543 public boolean hasNextBoolean()
545 Returns true if the next token in this scanner's input can be interpreted as a
546 boolean value using a case insensitive pattern created from the string
547 "true|false". The scanner does not advance past the input that matched.
550 Returns: true if and only if this scanner's next token is a valid boolean value
551 *java.util.Scanner.hasNextByte()*
553 public boolean hasNextByte()
555 Returns true if the next token in this scanner's input can be interpreted as a
556 byte value in the default radix using the (|java.util.Scanner|) method. The
557 scanner does not advance past any input.
560 Returns: true if and only if this scanner's next token is a valid byte value
561 *java.util.Scanner.hasNextByte(int)*
563 public boolean hasNextByte(int radix)
565 Returns true if the next token in this scanner's input can be interpreted as a
566 byte value in the specified radix using the (|java.util.Scanner|) method. The
567 scanner does not advance past any input.
569 radix - the radix used to interpret the token as a byte value
571 Returns: true if and only if this scanner's next token is a valid byte value
572 *java.util.Scanner.hasNextDouble()*
574 public boolean hasNextDouble()
576 Returns true if the next token in this scanner's input can be interpreted as a
577 double value using the (|java.util.Scanner|) method. The scanner does not
578 advance past any input.
581 Returns: true if and only if this scanner's next token is a valid double value
582 *java.util.Scanner.hasNextFloat()*
584 public boolean hasNextFloat()
586 Returns true if the next token in this scanner's input can be interpreted as a
587 float value using the (|java.util.Scanner|) method. The scanner does not
588 advance past any input.
591 Returns: true if and only if this scanner's next token is a valid float value
592 *java.util.Scanner.hasNextInt()*
594 public boolean hasNextInt()
596 Returns true if the next token in this scanner's input can be interpreted as an
597 int value in the default radix using the (|java.util.Scanner|) method. The
598 scanner does not advance past any input.
601 Returns: true if and only if this scanner's next token is a valid int value
602 *java.util.Scanner.hasNextInt(int)*
604 public boolean hasNextInt(int radix)
606 Returns true if the next token in this scanner's input can be interpreted as an
607 int value in the specified radix using the (|java.util.Scanner|) method. The
608 scanner does not advance past any input.
610 radix - the radix used to interpret the token as an int value
612 Returns: true if and only if this scanner's next token is a valid int value
613 *java.util.Scanner.hasNextLine()*
615 public boolean hasNextLine()
617 Returns true if there is another line in the input of this scanner. This method
618 may block while waiting for input. The scanner does not advance past any input.
621 Returns: true if and only if this scanner has another line of input
622 *java.util.Scanner.hasNextLong()*
624 public boolean hasNextLong()
626 Returns true if the next token in this scanner's input can be interpreted as a
627 long value in the default radix using the (|java.util.Scanner|) method. The
628 scanner does not advance past any input.
631 Returns: true if and only if this scanner's next token is a valid long value
632 *java.util.Scanner.hasNextLong(int)*
634 public boolean hasNextLong(int radix)
636 Returns true if the next token in this scanner's input can be interpreted as a
637 long value in the specified radix using the (|java.util.Scanner|) method. The
638 scanner does not advance past any input.
640 radix - the radix used to interpret the token as a long value
642 Returns: true if and only if this scanner's next token is a valid long value
643 *java.util.Scanner.hasNextShort()*
645 public boolean hasNextShort()
647 Returns true if the next token in this scanner's input can be interpreted as a
648 short value in the default radix using the (|java.util.Scanner|) method. The
649 scanner does not advance past any input.
652 Returns: true if and only if this scanner's next token is a valid short value in the
654 *java.util.Scanner.hasNextShort(int)*
656 public boolean hasNextShort(int radix)
658 Returns true if the next token in this scanner's input can be interpreted as a
659 short value in the specified radix using the (|java.util.Scanner|) method. The
660 scanner does not advance past any input.
662 radix - the radix used to interpret the token as a short value
664 Returns: true if and only if this scanner's next token is a valid short value in the
666 *java.util.Scanner.ioException()*
668 public |java.io.IOException| ioException()
670 Returns the IOException last thrown by this Scanner's underlying Readable. This
671 method returns null if no such exception exists.
674 Returns: the last exception thrown by this scanner's readable
675 *java.util.Scanner.locale()*
677 public |java.util.Locale| locale()
679 Returns this scanner's locale.
681 A scanner's locale affects many elements of its default primitive matching
682 regular expressions; see localized numbers above.
685 Returns: this scanner's locale
686 *java.util.Scanner.match()*
688 public |java.util.regex.MatchResult| match()
690 Returns the match result of the last scanning operation performed by this
691 scanner. This method throws IllegalStateException if no match has been
692 performed, or if the last match was not successful.
694 The various nextmethods of Scanner make a match result available if they
695 complete without throwing an exception. For instance, after an invocation of
696 the (|java.util.Scanner|) method that returned an int, this method returns a
697 MatchResult for the search of the Integer regular expression defined above.
698 Similarly the (|java.util.Scanner|) , (|java.util.Scanner|) , and
699 (|java.util.Scanner|) methods will make a match available if they succeed.
702 Returns: a match result for the last match operation
703 *java.util.Scanner.next()*
705 public |java.lang.String| next()
707 Finds and returns the next complete token from this scanner. A complete token
708 is preceded and followed by input that matches the delimiter pattern. This
709 method may block while waiting for input to scan, even if a previous invocation
710 of (|java.util.Scanner|) returned true.
713 Returns: the next token
714 *java.util.Scanner.next(Pattern)*
716 public |java.lang.String| next(java.util.regex.Pattern pattern)
718 Returns the next token if it matches the specified pattern. This method may
719 block while waiting for input to scan, even if a previous invocation of
720 (|java.util.Scanner|) returned true. If the match is successful, the scanner
721 advances past the input that matched the pattern.
723 pattern - the pattern to scan for
725 Returns: the next token
726 *java.util.Scanner.next(String)*
728 public |java.lang.String| next(java.lang.String pattern)
730 Returns the next token if it matches the pattern constructed from the specified
731 string. If the match is successful, the scanner advances past the input that
734 An invocation of this method of the form next(pattern) behaves in exactly the
735 same way as the invocation next(Pattern.compile(pattern)).
737 pattern - a string specifying the pattern to scan
739 Returns: the next token
740 *java.util.Scanner.nextBigDecimal()*
742 public |java.math.BigDecimal| nextBigDecimal()
744 Scans the next token of the input as a BigDecimal(|java.math.BigDecimal|) .
746 If the next token matches the Decimal regular expression defined above then the
747 token is converted into a BigDecimal value as if by removing all group
748 separators, mapping non-ASCII digits into ASCII digits via the
749 Character.digit(|java.lang.Character|) , and passing the resulting string to
750 the BigDecimal(String)(|java.math.BigDecimal|) constructor.
753 Returns: the BigDecimal scanned from the input
754 *java.util.Scanner.nextBigInteger()*
756 public |java.math.BigInteger| nextBigInteger()
758 Scans the next token of the input as a BigInteger(|java.math.BigInteger|) .
760 An invocation of this method of the form nextBigInteger() behaves in exactly
761 the same way as the invocation nextBigInteger(radix), where radix is the
762 default radix of this scanner.
765 Returns: the BigInteger scanned from the input
766 *java.util.Scanner.nextBigInteger(int)*
768 public |java.math.BigInteger| nextBigInteger(int radix)
770 Scans the next token of the input as a BigInteger(|java.math.BigInteger|) .
772 If the next token matches the Integer regular expression defined above then the
773 token is converted into a BigInteger value as if by removing all group
774 separators, mapping non-ASCII digits into ASCII digits via the
775 Character.digit(|java.lang.Character|) , and passing the resulting string to
776 the BigInteger(String, int)(|java.math.BigInteger|) constructor with the
779 radix - the radix used to interpret the token
781 Returns: the BigInteger scanned from the input
782 *java.util.Scanner.nextBoolean()*
784 public boolean nextBoolean()
786 Scans the next token of the input into a boolean value and returns that value.
787 This method will throw InputMismatchException if the next token cannot be
788 translated into a valid boolean value. If the match is successful, the scanner
789 advances past the input that matched.
792 Returns: the boolean scanned from the input
793 *java.util.Scanner.nextByte()*
795 public byte nextByte()
797 Scans the next token of the input as a byte.
799 An invocation of this method of the form nextByte() behaves in exactly the same
800 way as the invocation nextByte(radix), where radix is the default radix of this
804 Returns: the byte scanned from the input
805 *java.util.Scanner.nextByte(int)*
807 public byte nextByte(int radix)
809 Scans the next token of the input as a byte. This method will throw
810 InputMismatchException if the next token cannot be translated into a valid byte
811 value as described below. If the translation is successful, the scanner
812 advances past the input that matched.
814 If the next token matches the Integer regular expression defined above then the
815 token is converted into a byte value as if by removing all locale specific
816 prefixes, group separators, and locale specific suffixes, then mapping
817 non-ASCII digits into ASCII digits via Character.digit(|java.lang.Character|) ,
818 prepending a negative sign (-) if the locale specific negative prefixes and
819 suffixes were present, and passing the resulting string to
820 Byte.parseByte(|java.lang.Byte|) with the specified radix.
822 radix - the radix used to interpret the token as a byte value
824 Returns: the byte scanned from the input
825 *java.util.Scanner.nextDouble()*
827 public double nextDouble()
829 Scans the next token of the input as a double. This method will throw
830 InputMismatchException if the next token cannot be translated into a valid
831 double value. If the translation is successful, the scanner advances past the
834 If the next token matches the Float regular expression defined above then the
835 token is converted into a double value as if by removing all locale specific
836 prefixes, group separators, and locale specific suffixes, then mapping
837 non-ASCII digits into ASCII digits via Character.digit(|java.lang.Character|) ,
838 prepending a negative sign (-) if the locale specific negative prefixes and
839 suffixes were present, and passing the resulting string to
840 Double.parseDouble(|java.lang.Double|) . If the token matches the localized NaN
841 or infinity strings, then either "Nan" or "Infinity" is passed to
842 Double.parseDouble(|java.lang.Double|) as appropriate.
845 Returns: the double scanned from the input
846 *java.util.Scanner.nextFloat()*
848 public float nextFloat()
850 Scans the next token of the input as a float. This method will throw
851 InputMismatchException if the next token cannot be translated into a valid
852 float value as described below. If the translation is successful, the scanner
853 advances past the input that matched.
855 If the next token matches the Float regular expression defined above then the
856 token is converted into a float value as if by removing all locale specific
857 prefixes, group separators, and locale specific suffixes, then mapping
858 non-ASCII digits into ASCII digits via Character.digit(|java.lang.Character|) ,
859 prepending a negative sign (-) if the locale specific negative prefixes and
860 suffixes were present, and passing the resulting string to
861 Float.parseFloat(|java.lang.Float|) . If the token matches the localized NaN or
862 infinity strings, then either "Nan" or "Infinity" is passed to
863 Float.parseFloat(|java.lang.Float|) as appropriate.
866 Returns: the float scanned from the input
867 *java.util.Scanner.nextInt()*
871 Scans the next token of the input as an int.
873 An invocation of this method of the form nextInt() behaves in exactly the same
874 way as the invocation nextInt(radix), where radix is the default radix of this
878 Returns: the int scanned from the input
879 *java.util.Scanner.nextInt(int)*
881 public int nextInt(int radix)
883 Scans the next token of the input as an int. This method will throw
884 InputMismatchException if the next token cannot be translated into a valid int
885 value as described below. If the translation is successful, the scanner
886 advances past the input that matched.
888 If the next token matches the Integer regular expression defined above then the
889 token is converted into an int value as if by removing all locale specific
890 prefixes, group separators, and locale specific suffixes, then mapping
891 non-ASCII digits into ASCII digits via Character.digit(|java.lang.Character|) ,
892 prepending a negative sign (-) if the locale specific negative prefixes and
893 suffixes were present, and passing the resulting string to
894 Integer.parseInt(|java.lang.Integer|) with the specified radix.
896 radix - the radix used to interpret the token as an int value
898 Returns: the int scanned from the input
899 *java.util.Scanner.nextLine()*
901 public |java.lang.String| nextLine()
903 Advances this scanner past the current line and returns the input that was
906 This method returns the rest of the current line, excluding any line separator
907 at the end. The position is set to the beginning of the next line.
909 Since this method continues to search through the input looking for a line
910 separator, it may buffer all of the input searching for the line to skip if no
911 line separators are present.
914 Returns: the line that was skipped
915 *java.util.Scanner.nextLong()*
917 public long nextLong()
919 Scans the next token of the input as a long.
921 An invocation of this method of the form nextLong() behaves in exactly the same
922 way as the invocation nextLong(radix), where radix is the default radix of this
926 Returns: the long scanned from the input
927 *java.util.Scanner.nextLong(int)*
929 public long nextLong(int radix)
931 Scans the next token of the input as a long. This method will throw
932 InputMismatchException if the next token cannot be translated into a valid long
933 value as described below. If the translation is successful, the scanner
934 advances past the input that matched.
936 If the next token matches the Integer regular expression defined above then the
937 token is converted into an long value as if by removing all locale specific
938 prefixes, group separators, and locale specific suffixes, then mapping
939 non-ASCII digits into ASCII digits via Character.digit(|java.lang.Character|) ,
940 prepending a negative sign (-) if the locale specific negative prefixes and
941 suffixes were present, and passing the resulting string to
942 Long.parseLong(|java.lang.Long|) with the specified radix.
944 radix - the radix used to interpret the token as an int value
946 Returns: the long scanned from the input
947 *java.util.Scanner.nextShort()*
949 public short nextShort()
951 Scans the next token of the input as a short.
953 An invocation of this method of the form nextShort() behaves in exactly the
954 same way as the invocation nextShort(radix), where radix is the default radix
958 Returns: the short scanned from the input
959 *java.util.Scanner.nextShort(int)*
961 public short nextShort(int radix)
963 Scans the next token of the input as a short. This method will throw
964 InputMismatchException if the next token cannot be translated into a valid
965 short value as described below. If the translation is successful, the scanner
966 advances past the input that matched.
968 If the next token matches the Integer regular expression defined above then the
969 token is converted into a short value as if by removing all locale specific
970 prefixes, group separators, and locale specific suffixes, then mapping
971 non-ASCII digits into ASCII digits via Character.digit(|java.lang.Character|) ,
972 prepending a negative sign (-) if the locale specific negative prefixes and
973 suffixes were present, and passing the resulting string to
974 Short.parseShort(|java.lang.Short|) with the specified radix.
976 radix - the radix used to interpret the token as a short value
978 Returns: the short scanned from the input
979 *java.util.Scanner.radix()*
983 Returns this scanner's default radix.
985 A scanner's radix affects elements of its default number matching regular
986 expressions; see localized numbers above.
989 Returns: the default radix of this scanner
990 *java.util.Scanner.remove()*
994 The remove operation is not supported by this implementation of Iterator.
997 *java.util.Scanner.skip(Pattern)*
999 public |java.util.Scanner| skip(java.util.regex.Pattern pattern)
1001 Skips input that matches the specified pattern, ignoring delimiters. This
1002 method will skip input if an anchored match of the specified pattern succeeds.
1004 If a match to the specified pattern is not found at the current position, then
1005 no input is skipped and a NoSuchElementException is thrown.
1007 Since this method seeks to match the specified pattern starting at the
1008 scanner's current position, patterns that can match a lot of input (".*", for
1009 example) may cause the scanner to buffer a large amount of input.
1011 Note that it is possible to skip something without risking a
1012 NoSuchElementException by using a pattern that can match nothing, e.g.,
1015 pattern - a string specifying the pattern to skip over
1018 *java.util.Scanner.skip(String)*
1020 public |java.util.Scanner| skip(java.lang.String pattern)
1022 Skips input that matches a pattern constructed from the specified string.
1024 An invocation of this method of the form skip(pattern) behaves in exactly the
1025 same way as the invocation skip(Pattern.compile(pattern)).
1027 pattern - a string specifying the pattern to skip over
1030 *java.util.Scanner.toString()*
1032 public |java.lang.String| toString()
1034 Returns the string representation of this Scanner. The string representation of
1035 a Scanner contains information that may be useful for debugging. The exact
1036 format is unspecified.
1039 Returns: The string representation of this scanner
1040 *java.util.Scanner.useDelimiter(Pattern)*
1042 public |java.util.Scanner| useDelimiter(java.util.regex.Pattern pattern)
1044 Sets this scanner's delimiting pattern to the specified pattern.
1046 pattern - A delimiting pattern
1049 *java.util.Scanner.useDelimiter(String)*
1051 public |java.util.Scanner| useDelimiter(java.lang.String pattern)
1053 Sets this scanner's delimiting pattern to a pattern constructed from the
1056 An invocation of this method of the form useDelimiter(pattern) behaves in
1057 exactly the same way as the invocation hasDelimiter(Pattern.compile(pattern)).
1059 pattern - A string specifying a delimiting pattern
1062 *java.util.Scanner.useLocale(Locale)*
1064 public |java.util.Scanner| useLocale(java.util.Locale locale)
1066 Sets this scanner's locale to the specified locale.
1068 A scanner's locale affects many elements of its default primitive matching
1069 regular expressions; see localized numbers above.
1071 locale - A string specifying the locale to use
1074 *java.util.Scanner.useRadix(int)*
1076 public |java.util.Scanner| useRadix(int radix)
1078 Sets this scanner's default radix to the specified radix.
1080 A scanner's radix affects elements of its default number matching regular
1081 expressions; see localized numbers above.
1083 If the radix is less than Character.MIN_RADIX or greater than
1084 Character.MAX_RADIX, then an IllegalArgumentException is thrown.
1086 radix - The radix to use when scanning numbers