public final class Scanner
extends Object implements Iterator<String>, Closeable
| java.lang.Object | |
| java.util.Scanner | |
一个简单的文本扫描器,它可以使用正则表达式分析原始类型和字符串。
A Scanner使用定界符模式将其输入分为标记,默认情况下该定界符与空白相匹配。 将得到的令牌可以然后被转换成使用各种方法next不同类型的值。
例如,此代码允许用户从 System.in读取一个数字:
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
作为另一个例子,该代码允许 long类型从文件 myNumbers条目分配:
Scanner sc = new Scanner(new File("myNumbers"));
while (sc.hasNextLong()) {
long aLong = sc.nextLong();
}
扫描器也可以使用除空白以外的分隔符。 本示例从字符串中读取几个项目:
String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close();
打印以下输出:
1
2
red
blue
使用此代码可以生成相同的输出,该代码使用正则表达式一次解析所有四个标记:
String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input);
s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
MatchResult result = s.match();
for (int i=1; i<=result.groupCount(); i++)
System.out.println(result.group(i));
s.close();
扫描仪使用的default whitespace delimiter被认定为Character 。 isWhitespace 。 reset()方法会将扫描器分隔符的值重置为默认空白分隔符,无论以前是否更改过。
扫描操作可能会阻止等待输入。
next()和hasNext()方法及其基元类型伴随方法(如nextInt()和hasNextInt() )首先跳过与分隔符模式匹配的任何输入,然后尝试返回下一个标记。 方法hasNext和next都可能阻止等待进一步的输入。 hasNext方法块与其关联的next方法是否会阻塞无关 。
的findInLine(String) , findWithinHorizon(String, int)和skip(String)方法定界符模式的独立操作。 这些方法将尝试匹配指定的模式,而不考虑输入中的分隔符,因此可以在分隔符不相关的特殊情况下使用。 这些方法可能会阻止等待更多输入。
当扫描器抛出 InputMismatchException ,扫描器不会传递导致该异常的令牌,以便通过其他方法检索或跳过该令牌。
根据分隔模式的类型,可能会返回空的标记。 例如,模式"\\s+"将返回没有空的标记,因为它匹配分隔符的多个实例。 分隔模式"\\s"可能会返回空标记,因为它每次只传递一个空格。
扫描仪可以从任何实现Readable接口的对象中读取文本。 如果调用底层可读的read(CharBuffer)方法抛出IOException则扫描器会假定输入的末尾已到达。 底层可读的最新IOException可以通过ioException()方法检索。
当 Scanner关闭时,如果源实现 Closeable接口,它将关闭其输入源。
如果没有外部同步, Scanner对于多线程应用并不安全。
除非另有提及,传递 null参数成的任何方法 Scanner将导致 NullPointerException被抛出。
扫描仪将默认将数字解释为十进制数,除非已使用useRadix(int)方法设置了不同的基数。 无论先前是否更改, reset()方法都会将扫描仪基数的值重置为10 。
An instance of this class is capable of scanning numbers in the standard formats as well as in the formats of the scanner's locale. A scanner's initial locale是getDefault()方法返回的值; 它可能通过useLocale(Locale)方法更改。 reset()方法会将扫描程序语言环境的值重置为初始语言环境,而不管以前是否更改过。
本地化格式根据以下参数定义,对于特定语言环境,取自该语言环境的 DecimalFormat对象, df及其对象和 DecimalFormatSymbols对象, dfs 。
LocalGroupSeparator The character used to separate thousands groups, i.e., dfs. getGroupingSeparator()LocalDecimalSeparator The character used for the decimal point, i.e., dfs. getDecimalSeparator()LocalPositivePrefix The string that appears before a positive number (may be empty), i.e., df. getPositivePrefix()LocalPositiveSuffix The string that appears after a positive number (may be empty), i.e., df. getPositiveSuffix()LocalNegativePrefix The string that appears before a negative number (may be empty), i.e., df. getNegativePrefix()LocalNegativeSuffix The string that appears after a negative number (may be empty), i.e., df. getNegativeSuffix()LocalNaN The string that represents not-a-number for floating-point values, i.e., dfs. getNaN()LocalInfinity The string that represents infinity for floating-point values, i.e., dfs. getInfinity()
可以通过此类的实例解析为数字的字符串按照以下正则表达式语法指定,其中Rmax是所用基数中最高的数字(例如,Rmax在基数10中为9)。
| NonASCIIDigit :: | = A non-ASCII character c for which Character.isDigit(c) returns true |
||||
| Non0Digit :: | = [1-Rmax] | NonASCIIDigit | ||||
| Digit :: | = [0-Rmax] | NonASCIIDigit | ||||
| GroupedNumeral :: |
|
||||
| Numeral :: | = ( ( Digit+ ) | GroupedNumeral ) | ||||
| Integer :: | = ( [-+]? ( Numeral ) ) | ||||
| | LocalPositivePrefix Numeral LocalPositiveSuffix | |||||
| | LocalNegativePrefix Numeral LocalNegativeSuffix | |||||
| DecimalNumeral :: | = Numeral | ||||
| | Numeral LocalDecimalSeparator Digit* | |||||
| | LocalDecimalSeparator Digit+ | |||||
| Exponent :: | = ( [eE] [+-]? Digit+ ) | ||||
| Decimal :: | = ( [-+]? DecimalNumeral Exponent? ) | ||||
| | LocalPositivePrefix DecimalNumeral LocalPositiveSuffix Exponent? | |||||
| | LocalNegativePrefix DecimalNumeral LocalNegativeSuffix Exponent? | |||||
| HexFloat :: | = [-+]? 0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+ ([pP][-+]?[0-9]+)? | ||||
| NonNumber :: | = NaN | LocalNan | Infinity | LocalInfinity | ||||
| SignedNonNumber :: | = ( [-+]? NonNumber ) | ||||
| | LocalPositivePrefix NonNumber LocalPositiveSuffix | |||||
| | LocalNegativePrefix NonNumber LocalNegativeSuffix | |||||
| Float :: | = Decimal | ||||
| | HexFloat | |||||
| | SignedNonNumber |
上述正则表达式中的空格不重要。
Public constructors |
|
|---|---|
Scanner(Readable source) 构造一个新的 |
|
Scanner(InputStream source) 构造一个新的 |
|
Scanner(InputStream source, String charsetName) 构造一个新的 |
|
Scanner(File source) 构造一个新的 |
|
Scanner(File source, String charsetName) 构造一个新的 |
|
Scanner(String source) 构造一个新的 |
|
Scanner(ReadableByteChannel source) 构造一个新的 |
|
Scanner(ReadableByteChannel source, String charsetName) 构造一个新的 |
|
公共方法(Public methods) |
|
|---|---|
void |
close() 关闭此扫描仪。 |
Pattern |
delimiter() 返回 |
String |
findInLine(String pattern) 尝试找到从指定字符串构造的模式的下一次出现,忽略分隔符。 |
String |
findInLine(Pattern pattern) 尝试查找忽略分隔符的指定模式的下一次出现。 |
String |
findWithinHorizon(Pattern pattern, int horizon) 尝试找到指定模式的下一次出现。 |
String |
findWithinHorizon(String pattern, int horizon) 尝试找到从指定字符串构造的模式的下一次出现,忽略分隔符。 |
boolean |
hasNext() 如果此扫描器在其输入中有另一个标记,则返回true。 |
boolean |
hasNext(String pattern) 如果下一个标记与从指定字符串构造的模式匹配,则返回true。 |
boolean |
hasNext(Pattern pattern) 如果下一个完整标记匹配指定的模式,则返回true。 |
boolean |
hasNextBigDecimal() 如果此扫描器输入中的下一个标记可以使用 |
boolean |
hasNextBigInteger(int radix) 如果使用 |
boolean |
hasNextBigInteger() 如果此扫描器输入中的下一个标记可以使用 |
boolean |
hasNextBoolean() 如果此扫描器输入中的下一个标记可以使用从字符串“true | false”创建的不区分大小写的模式被解释为布尔值,则返回true。 |
boolean |
hasNextByte() 如果此扫描器输入中的下一个标记可以解释为使用 |
boolean |
hasNextByte(int radix) 如果此扫描器输入中的下一个标记可以使用 |
boolean |
hasNextDouble() 如果此扫描器输入中的下一个标记可以使用 |
boolean |
hasNextFloat() 如果此扫描器输入中的下一个标记可以使用 |
boolean |
hasNextInt() 如果此扫描器输入中的下一个标记可以使用 |
boolean |
hasNextInt(int radix) 如果使用 |
boolean |
hasNextLine() 如果此扫描仪的输入中有另一行,则返回true。 |
boolean |
hasNextLong() 如果使用 |
boolean |
hasNextLong(int radix) 如果使用 |
boolean |
hasNextShort() 如果使用 |
boolean |
hasNextShort(int radix) 如果使用 |
IOException |
ioException() 返回 |
Locale |
locale() 返回此扫描仪的语言环境。 |
MatchResult |
match() 返回此扫描仪执行的上次扫描操作的匹配结果。 |
String |
next(Pattern pattern) 如果它与指定的模式匹配,则返回下一个标记。 |
String |
next() 查找并返回此扫描程序中的下一个完整标记。 |
String |
next(String pattern) 如果它与从指定字符串构造的模式相匹配,则返回下一个标记。 |
BigDecimal |
nextBigDecimal() 扫描输入的下一个标记为 |
BigInteger |
nextBigInteger() 扫描输入的下一个标记为 |
BigInteger |
nextBigInteger(int radix) 扫描输入的下一个标记为 |
boolean |
nextBoolean() 将输入的下一个标记扫描为布尔值并返回该值。 |
byte |
nextByte(int radix) 扫描输入的下一个标记为 byte 。 |
byte |
nextByte() 扫描输入的下一个标记为 byte 。 |
double |
nextDouble() 扫描输入的下一个标记为 double 。 |
float |
nextFloat() 扫描输入的下一个标记为 float 。 |
int |
nextInt() 扫描输入的下一个标记为 int 。 |
int |
nextInt(int radix) 扫描输入的下一个标记为 int 。 |
String |
nextLine() 将此扫描器推进到当前行并返回跳过的输入。 |
long |
nextLong() 扫描输入的下一个标记为 long 。 |
long |
nextLong(int radix) 扫描输入的下一个标记为 long 。 |
short |
nextShort(int radix) 将输入的下一个标记扫描为 short 。 |
short |
nextShort() 扫描输入的下一个标记为 short 。 |
int |
radix() 返回此扫描仪的默认基数。 |
void |
remove() 此实现 |
Scanner |
reset() 重置此扫描仪。 |
Scanner |
skip(String pattern) 跳过与从指定字符串构造的模式相匹配的输入。 |
Scanner |
skip(Pattern pattern) 跳过与指定模式匹配的输入,忽略分隔符。 |
String |
toString() 返回此 |
Scanner |
useDelimiter(Pattern pattern) 将此扫描仪的分隔模式设置为指定模式。 |
Scanner |
useDelimiter(String pattern) 将此扫描仪的分隔图案设置为从指定的 |
Scanner |
useLocale(Locale locale) 将此扫描仪的语言环境设置为指定的语言环境。 |
Scanner |
useRadix(int radix) 将此扫描仪的默认基数设置为指定的基数。 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
java.util.Iterator
|
|
java.io.Closeable
|
|
java.lang.AutoCloseable
|
|
Scanner (Readable source)
构造一个新的 Scanner ,生成从指定源扫描的值。
| 参数(Parameters) | |
|---|---|
source |
Readable: A character source implementing the Readable interface |
Scanner (InputStream source)
构造一个新的Scanner ,它产生从指定输入流扫描的值。 使用底层平台的default charset将流中的字节转换为字符。
| 参数(Parameters) | |
|---|---|
source |
InputStream: An input stream to be scanned |
Scanner (InputStream source, String charsetName)
构造一个新的Scanner ,产生从指定输入流扫描的值。 使用指定的字符集将流中的字节转换为字符。
| 参数(Parameters) | |
|---|---|
source |
InputStream: An input stream to be scanned |
charsetName |
String: The encoding type used to convert bytes from the stream into characters to be scanned |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if the specified character set does not exist |
Scanner (File source)
构造一个新的Scanner ,产生从指定文件扫描的值。 使用底层平台default charset将文件中的字节转换为字符。
| 参数(Parameters) | |
|---|---|
source |
File: A file to be scanned |
| 抛出异常(Throws) | |
|---|---|
FileNotFoundException |
if source is not found |
Scanner (File source, String charsetName)
构造一个新的Scanner ,产生从指定文件扫描的值。 使用指定的字符集将文件中的字节转换为字符。
| 参数(Parameters) | |
|---|---|
source |
File: A file to be scanned |
charsetName |
String: The encoding type used to convert bytes from the file into characters to be scanned |
| 抛出异常(Throws) | |
|---|---|
FileNotFoundException |
if source is not found |
IllegalArgumentException |
if the specified encoding is not found |
Scanner (String source)
构造一个新的 Scanner ,它产生从指定字符串扫描的值。
| 参数(Parameters) | |
|---|---|
source |
String: A string to scan |
Scanner (ReadableByteChannel source)
构造一个新的Scanner ,产生从指定通道扫描的值。 使用底层平台的default charset将来自源的字节转换为字符。
| 参数(Parameters) | |
|---|---|
source |
ReadableByteChannel: A channel to scan |
Scanner (ReadableByteChannel source, String charsetName)
构造一个新的Scanner ,产生从指定通道扫描的值。 来自源的字节使用指定的字符集转换为字符。
| 参数(Parameters) | |
|---|---|
source |
ReadableByteChannel: A channel to scan |
charsetName |
String: The encoding type used to convert bytes from the channel into characters to be scanned |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if the specified character set does not exist |
void close ()
关闭此扫描仪。
如果这个扫描器还没有关闭,那么如果它的底层readable也实现了Closeable接口,那么可读的close方法将被调用。 如果此扫描程序已关闭,则调用此方法将不起作用。
试图在扫描仪关闭后执行搜索操作将导致 IllegalStateException 。
Pattern delimiter ()
返回 Pattern这 Scanner当前用于匹配分隔符。
| 返回(Returns) | |
|---|---|
Pattern |
this scanner's delimiting pattern. |
String findInLine (String pattern)
尝试找到从指定字符串构造的模式的下一次出现,忽略分隔符。
表单 findInLine(pattern)的此方法的调用的行为方式与调用 findInLine(Pattern.compile(pattern))的方式 完全相同 。
| 参数(Parameters) | |
|---|---|
pattern |
String: a string specifying the pattern to search for |
| 返回(Returns) | |
|---|---|
String |
the text that matched the specified pattern |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
String findInLine (Pattern pattern)
尝试查找忽略分隔符的指定模式的下一次出现。 如果在下一行分隔符之前找到该模式,则扫描程序超前匹配的输入并返回与该模式匹配的字符串。 如果在直到下一行分隔符的输入中没有检测到这种模式,则返回null ,并且扫描仪的位置不变。 此方法可能会阻止等待与模式匹配的输入。
由于此方法继续搜索输入以查找指定模式,因此如果没有行分隔符存在,它可能会缓冲搜索所需标记的所有输入。
| 参数(Parameters) | |
|---|---|
pattern |
Pattern: the pattern to scan for |
| 返回(Returns) | |
|---|---|
String |
the text that matched the specified pattern |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
String findWithinHorizon (Pattern pattern, int horizon)
尝试找到指定模式的下一次出现。
此方法搜索输入直至指定的搜索范围,忽略分隔符。 如果找到该模式,则扫描仪超过匹配的输入并返回匹配该模式的字符串。 如果没有检测到这种模式,则返回null,并且扫描仪的位置保持不变。 此方法可能会阻止等待与模式匹配的输入。
扫描仪不会搜索超过其当前位置的超过horizon代码点。 请注意,比赛可能会被地平线裁剪; 也就是说,如果视野更大,任意的匹配结果可能会有所不同。 扫描器将地平线视为透明的非锚定边界(请参阅useTransparentBounds(boolean)和useAnchoringBounds(boolean) )。
如果水平线为0 ,那么水平线将被忽略,并且此方法继续在输入中搜索,以查找无限制的指定模式。 在这种情况下,它可以缓冲搜索模式的所有输入。
如果地平线为负数,则会抛出IllegalArgumentException。
| 参数(Parameters) | |
|---|---|
pattern |
Pattern: the pattern to scan for |
horizon |
int
|
| 返回(Returns) | |
|---|---|
String |
the text that matched the specified pattern |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
IllegalArgumentException |
if horizon is negative |
String findWithinHorizon (String pattern, int horizon)
尝试找到从指定字符串构造的模式的下一次出现,忽略分隔符。
形式 findWithinHorizon(pattern)的这种方法的调用行为以完全相同的方式调用 findWithinHorizon(Pattern.compile(pattern, horizon))。
| 参数(Parameters) | |
|---|---|
pattern |
String: a string specifying the pattern to search for |
horizon |
int
|
| 返回(Returns) | |
|---|---|
String |
the text that matched the specified pattern |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
IllegalArgumentException |
if horizon is negative |
boolean hasNext ()
如果此扫描器在其输入中有另一个标记,则返回true。 此方法可能会在等待输入进行扫描时阻塞。 扫描仪不会超过任何输入。
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner has another token |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
也可以看看:
boolean hasNext (String pattern)
如果下一个标记与从指定字符串构造的模式匹配,则返回true。 扫描仪不会超过任何输入。
调用表单 hasNext(pattern)的此方法的行为与调用 hasNext(Pattern.compile(pattern))的行为完全相同。
| 参数(Parameters) | |
|---|---|
pattern |
String: a string specifying the pattern to scan |
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner has another token matching the specified pattern |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
boolean hasNext (Pattern pattern)
如果下一个完整标记匹配指定的模式,则返回true。 一个完整的标记由与分隔符模式匹配的输入前缀和后缀。 此方法可能会在等待输入时阻塞。 扫描仪不会超过任何输入。
| 参数(Parameters) | |
|---|---|
pattern |
Pattern: the pattern to scan for |
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner has another token matching the specified pattern |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
boolean hasNextBigDecimal ()
如果此扫描器输入中的下一个标记可以使用nextBigDecimal()方法解释为BigDecimal则返回true。 扫描仪不会超过任何输入。
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner's next token is a valid BigDecimal |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
boolean hasNextBigInteger (int radix)
如果此扫描器输入中的下一个标记可以使用nextBigInteger()方法解释为指定基数的BigInteger ,则返回true。 扫描仪不会超过任何输入。
| 参数(Parameters) | |
|---|---|
radix |
int: the radix used to interpret the token as an integer |
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner's next token is a valid BigInteger |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
boolean hasNextBigInteger ()
如果此扫描器输入中的下一个标记可以使用nextBigInteger()方法解释为默认基数的BigInteger ,则返回true。 扫描仪不会超过任何输入。
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner's next token is a valid BigInteger |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
boolean hasNextBoolean ()
如果此扫描器输入中的下一个标记可以使用从字符串“true | false”创建的不区分大小写的模式被解释为布尔值,则返回true。 扫描仪不会超过匹配的输入。
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner's next token is a valid boolean value |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
boolean hasNextByte ()
如果此扫描器输入中的下一个标记可以使用nextByte()方法解释为默认基数中的字节值,则返回true。 扫描仪不会超过任何输入。
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner's next token is a valid byte value |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
boolean hasNextByte (int radix)
如果此扫描器输入中的下一个标记可以使用方法nextByte()解释为指定基数中的字节值,则返回true。 扫描仪不会超过任何输入。
| 参数(Parameters) | |
|---|---|
radix |
int: the radix used to interpret the token as a byte value |
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner's next token is a valid byte value |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
boolean hasNextDouble ()
如果此扫描器输入中的下一个标记可以使用nextDouble()方法解释为双nextDouble()值,则返回true。 扫描仪不会超过任何输入。
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner's next token is a valid double value |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
boolean hasNextFloat ()
如果此扫描器输入中的下一个标记可以使用nextFloat()方法解释为浮点值,则返回true。 扫描仪不会超过任何输入。
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner's next token is a valid float value |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
boolean hasNextInt ()
如果此扫描器输入中的下一个标记可使用nextInt()方法解释为默认基数中的nextInt()数值,则返回true。 扫描仪不会超过任何输入。
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner's next token is a valid int value |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
boolean hasNextInt (int radix)
如果此扫描器输入中的下一个标记可以使用nextInt()方法解释为指定基数中的int值,则返回true。 扫描仪不会超过任何输入。
| 参数(Parameters) | |
|---|---|
radix |
int: the radix used to interpret the token as an int value |
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner's next token is a valid int value |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
boolean hasNextLine ()
如果此扫描仪的输入中有另一行,则返回true。 此方法可能会在等待输入时阻塞。 扫描仪不会超过任何输入。
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner has another line of input |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
boolean hasNextLong ()
如果使用nextLong()方法,可以将此扫描器输入中的下一个标记解释为默认基数中的长nextLong()值,则返回true。 扫描仪不会超过任何输入。
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner's next token is a valid long value |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
boolean hasNextLong (int radix)
如果使用nextLong()方法,此扫描器输入中的下一个标记可被解释为指定基数中的长nextLong()值,则返回true。 扫描仪不会超过任何输入。
| 参数(Parameters) | |
|---|---|
radix |
int: the radix used to interpret the token as a long value |
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner's next token is a valid long value |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
boolean hasNextShort ()
如果使用nextShort()方法,此扫描器输入中的下一个标记可被解释为默认基数中的短值,则返回true。 扫描仪不会超过任何输入。
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner's next token is a valid short value in the default radix |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
boolean hasNextShort (int radix)
如果使用方法nextShort()可以将此扫描器输入中的下一个标记解释为指定基数中的短值,则返回true。 扫描仪不会超过任何输入。
| 参数(Parameters) | |
|---|---|
radix |
int: the radix used to interpret the token as a short value |
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this scanner's next token is a valid short value in the specified radix |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
IOException ioException ()
返回IOException最后通过此抛出Scanner的基本Readable 。 如果不存在此类异常,则此方法返回null 。
| 返回(Returns) | |
|---|---|
IOException |
the last exception thrown by this scanner's readable |
Locale locale ()
返回此扫描仪的语言环境。
扫描仪的语言环境会影响其默认基元匹配正则表达式的许多元素; 见上面的localized numbers 。
| 返回(Returns) | |
|---|---|
Locale |
this scanner's locale |
MatchResult match ()
返回此扫描仪执行的上次扫描操作的匹配结果。 如果未执行匹配,或者上次匹配IllegalStateException则此方法将引发IllegalStateException 。
各种next的方法Scanner使比赛结果可用,如果他们完成未抛出异常。 例如,在调用返回int的nextInt()方法后,此方法返回MatchResult以搜索上面定义的Integer正则表达式。 同样, findInLine(String) , findWithinHorizon(String, int) ,并skip(String)方法将一个匹配结果,如果他们取得成功。
| 返回(Returns) | |
|---|---|
MatchResult |
a match result for the last match operation |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
If no match result is available |
String next (Pattern pattern)
如果它与指定的模式匹配,则返回下一个标记。 即使先前调用hasNext(Pattern)返回true ,此方法也可能在等待输入进行扫描时true 。 如果匹配成功,则扫描仪前进超过与模式匹配的输入。
| 参数(Parameters) | |
|---|---|
pattern |
Pattern: the pattern to scan for |
| 返回(Returns) | |
|---|---|
String |
the next token |
| 抛出异常(Throws) | |
|---|---|
NoSuchElementException |
if no more tokens are available |
IllegalStateException |
if this scanner is closed |
String next ()
查找并返回此扫描程序中的下一个完整标记。 完整的令牌前后有与分隔符模式匹配的输入。 即使先前调用hasNext()返回true ,此方法可能会在等待输入进行扫描时true 。
| 返回(Returns) | |
|---|---|
String |
the next token |
| 抛出异常(Throws) | |
|---|---|
NoSuchElementException |
if no more tokens are available |
IllegalStateException |
if this scanner is closed |
也可以看看:
String next (String pattern)
如果它与从指定字符串构造的模式相匹配,则返回下一个标记。 如果匹配成功,则扫描仪前进超过与模式匹配的输入。
调用表单 next(pattern)的此方法的行为与调用 next(Pattern.compile(pattern))的行为完全相同。
| 参数(Parameters) | |
|---|---|
pattern |
String: a string specifying the pattern to scan |
| 返回(Returns) | |
|---|---|
String |
the next token |
| 抛出异常(Throws) | |
|---|---|
NoSuchElementException |
if no such tokens are available |
IllegalStateException |
if this scanner is closed |
BigDecimal nextBigDecimal ()
扫描输入的下一个标记为 BigDecimal 。
如果下一个标记与上面定义的 Decimal正则表达式相匹配,那么标记将转换为 BigDecimal值,就好像通过删除所有组分隔符,通过 Character.digit将非ASCII数字映射为ASCII数字,并将结果字符串传递给 BigDecimal(String)构造函数。
| 返回(Returns) | |
|---|---|
BigDecimal |
the BigDecimal scanned from the input |
| 抛出异常(Throws) | |
|---|---|
InputMismatchException |
if the next token does not match the Decimal regular expression, or is out of range |
NoSuchElementException |
if the input is exhausted |
IllegalStateException |
if this scanner is closed |
BigInteger nextBigInteger ()
扫描输入的下一个标记为 BigInteger 。
形式 nextBigInteger()的这种方法调用的行为完全相同的方式调用 nextBigInteger(radix),其中 radix是此扫描器的默认基数。
| 返回(Returns) | |
|---|---|
BigInteger |
the BigInteger scanned from the input |
| 抛出异常(Throws) | |
|---|---|
InputMismatchException |
if the next token does not match the Integer regular expression, or is out of range |
NoSuchElementException |
if the input is exhausted |
IllegalStateException |
if this scanner is closed |
BigInteger nextBigInteger (int radix)
扫描输入的下一个标记为 BigInteger 。
如果下一个标记与上面定义的 Integer正则表达式相匹配,那么标记将转换为 BigInteger值,就好像通过删除所有组分隔符,通过 Character.digit将非ASCII数字映射为ASCII数字,并将结果字符串传递给 BigInteger(String, int)构造函数指定的基数。
| 参数(Parameters) | |
|---|---|
radix |
int: the radix used to interpret the token |
| 返回(Returns) | |
|---|---|
BigInteger |
the BigInteger scanned from the input |
| 抛出异常(Throws) | |
|---|---|
InputMismatchException |
if the next token does not match the Integer regular expression, or is out of range |
NoSuchElementException |
if the input is exhausted |
IllegalStateException |
if this scanner is closed |
boolean nextBoolean ()
将输入的下一个标记扫描为布尔值并返回该值。 如果下一个标记无法转换为有效的布尔值,则此方法将抛出InputMismatchException 。 如果匹配成功,则扫描仪前进通过匹配的输入。
| 返回(Returns) | |
|---|---|
boolean |
the boolean scanned from the input |
| 抛出异常(Throws) | |
|---|---|
InputMismatchException |
if the next token is not a valid boolean |
NoSuchElementException |
if input is exhausted |
IllegalStateException |
if this scanner is closed |
byte nextByte (int radix)
扫描输入的下一个标记为byte 。 如果下一个标记无法转换为有效的字节值,则此方法将抛出InputMismatchException ,如下所述。 如果翻译成功,扫描仪将前进通过匹配的输入。
如果下一个标记与上面定义的 Integer正则表达式匹配,那么标记将被转换为 byte值,就好像通过删除所有特定于语言环境的前缀,组分隔符和特定于语言环境的后缀,然后通过 Character.digit将非ASCII数字映射为ASCII数字,如果特定区域特定的负前缀和后缀存在,则为负号( - ),并将结果字符串以指定的基数传递到 Byte.parseByte 。
| 参数(Parameters) | |
|---|---|
radix |
int: the radix used to interpret the token as a byte value |
| 返回(Returns) | |
|---|---|
byte |
the byte scanned from the input |
| 抛出异常(Throws) | |
|---|---|
InputMismatchException |
if the next token does not match the Integer regular expression, or is out of range |
NoSuchElementException |
if input is exhausted |
IllegalStateException |
if this scanner is closed |
byte nextByte ()
扫描输入的下一个标记为 byte 。
形式 nextByte()的这种方法调用的行为完全相同的方式调用 nextByte(radix),其中 radix是此扫描器的默认基数。
| 返回(Returns) | |
|---|---|
byte |
the byte scanned from the input |
| 抛出异常(Throws) | |
|---|---|
InputMismatchException |
if the next token does not match the Integer regular expression, or is out of range |
NoSuchElementException |
if input is exhausted |
IllegalStateException |
if this scanner is closed |
double nextDouble ()
扫描输入的下一个标记为double 。 如果下一个标记不能转换为有效的double值,则此方法将抛出InputMismatchException 。 如果翻译成功,扫描仪将前进通过匹配的输入。
如果下一个标记与上面定义的Float正则表达式相匹配,则标记转换为double值,就好像通过删除所有特定于语言环境的前缀,组分隔符和特定于语言环境的后缀,然后通过Character.digit将非ASCII数字映射为ASCII数字,如果特定于语言环境的负前缀和后缀存在,则为负号( - ),并将结果字符串传递给Double.parseDouble 。 如果令牌与本地化的NaN或无限字符串匹配,则将“Nan”或“Infinity”视情况传递给Double.parseDouble 。
| 返回(Returns) | |
|---|---|
double |
the double scanned from the input |
| 抛出异常(Throws) | |
|---|---|
InputMismatchException |
if the next token does not match the Float regular expression, or is out of range |
NoSuchElementException |
if the input is exhausted |
IllegalStateException |
if this scanner is closed |
float nextFloat ()
扫描输入的下一个标记为float 。 如果下一个标记无法转换为有效的浮点值,则此方法将抛出InputMismatchException ,如下所述。 如果翻译成功,扫描仪将前进通过匹配的输入。
如果下一个标记与上面定义的Float正则表达式相匹配,那么将标记转换为float值,就好像删除所有特定于语言环境的前缀,组分隔符和特定于语言环境的后缀,然后通过Character.digit将非ASCII数字映射为ASCII数字,如果特定于语言环境的负前缀和后缀存在,则为负号( - ),并将结果字符串传递给Float.parseFloat 。 如果令牌与本地化的NaN或无穷大字符串匹配,则将“Nan”或“Infinity”视情况传递给Float.parseFloat 。
| 返回(Returns) | |
|---|---|
float |
the float scanned from the input |
| 抛出异常(Throws) | |
|---|---|
InputMismatchException |
if the next token does not match the Float regular expression, or is out of range |
NoSuchElementException |
if input is exhausted |
IllegalStateException |
if this scanner is closed |
int nextInt ()
扫描输入的下一个标记为 int 。
形式 nextInt()的这种方法调用的行为完全相同的方式调用 nextInt(radix),其中 radix是此扫描器的默认基数。
| 返回(Returns) | |
|---|---|
int |
the int scanned from the input |
| 抛出异常(Throws) | |
|---|---|
InputMismatchException |
if the next token does not match the Integer regular expression, or is out of range |
NoSuchElementException |
if input is exhausted |
IllegalStateException |
if this scanner is closed |
int nextInt (int radix)
扫描输入的下一个标记为int 。 如果下一个标记无法转换为有效的int值,则此方法将抛出InputMismatchException ,如下所述。 如果翻译成功,扫描仪将前进通过匹配的输入。
如果下一个标记与上面定义的 Integer正则表达式相匹配,那么标记将转换为 int值,就好像删除所有特定于语言环境的前缀,组分隔符和特定于语言环境的后缀,然后通过 Character.digit将非ASCII数字映射为ASCII数字, Character.digit如果特定于语言环境的负前缀和后缀存在,则为负号( - ),并将结果字符串以指定的基数传递到 Integer.parseInt 。
| 参数(Parameters) | |
|---|---|
radix |
int: the radix used to interpret the token as an int value |
| 返回(Returns) | |
|---|---|
int |
the int scanned from the input |
| 抛出异常(Throws) | |
|---|---|
InputMismatchException |
if the next token does not match the Integer regular expression, or is out of range |
NoSuchElementException |
if input is exhausted |
IllegalStateException |
if this scanner is closed |
String nextLine ()
将此扫描器推进到当前行并返回跳过的输入。 此方法返回当前行的其余部分,排除末尾的任何行分隔符。 该位置设置为下一行的开头。
由于此方法继续在输入中搜索以查找行分隔符,因此如果没有行分隔符存在,它可能会缓存搜索要跳过的行的所有输入。
| 返回(Returns) | |
|---|---|
String |
the line that was skipped |
| 抛出异常(Throws) | |
|---|---|
NoSuchElementException |
if no line was found |
IllegalStateException |
if this scanner is closed |
long nextLong ()
扫描输入的下一个标记为 long 。
形式 nextLong()的这种方法调用的行为完全相同的方式调用 nextLong(radix),其中 radix是此扫描器的默认基数。
| 返回(Returns) | |
|---|---|
long |
the long scanned from the input |
| 抛出异常(Throws) | |
|---|---|
InputMismatchException |
if the next token does not match the Integer regular expression, or is out of range |
NoSuchElementException |
if input is exhausted |
IllegalStateException |
if this scanner is closed |
long nextLong (int radix)
扫描输入的下一个标记为long 。 如果下一个标记无法转换为有效的长InputMismatchException则此方法将抛出InputMismatchException ,如下所述。 如果翻译成功,扫描仪将前进通过匹配的输入。
如果下一个标记与上面定义的 Integer正则表达式相匹配,则标记转换为 long值,就好像通过删除所有特定于语言环境的前缀,组分隔符和特定于语言环境的后缀,然后通过 Character.digit将非ASCII数字映射为ASCII数字,如果特定区域特定的负前缀和后缀存在,则为负号( - ),并将结果字符串以指定的基数传递到 Long.parseLong 。
| 参数(Parameters) | |
|---|---|
radix |
int: the radix used to interpret the token as an int value |
| 返回(Returns) | |
|---|---|
long |
the long scanned from the input |
| 抛出异常(Throws) | |
|---|---|
InputMismatchException |
if the next token does not match the Integer regular expression, or is out of range |
NoSuchElementException |
if input is exhausted |
IllegalStateException |
if this scanner is closed |
short nextShort (int radix)
扫描输入的下一个标记为short 。 如果下一个标记无法转换为有效的短值,则此方法将抛出InputMismatchException ,如下所述。 如果翻译成功,扫描仪将前进通过匹配的输入。
如果下一个标记与上面定义的 Integer正则表达式相匹配,那么标记将转换为 short值,就好像通过删除所有特定于语言环境的前缀,组分隔符和特定于语言环境的后缀,然后通过 Character.digit将非ASCII数字映射为ASCII数字,如果特定区域特定的负前缀和后缀存在,则为负号( - ),并将结果字符串以指定的基数传递到 Short.parseShort 。
| 参数(Parameters) | |
|---|---|
radix |
int: the radix used to interpret the token as a short value |
| 返回(Returns) | |
|---|---|
short |
the short scanned from the input |
| 抛出异常(Throws) | |
|---|---|
InputMismatchException |
if the next token does not match the Integer regular expression, or is out of range |
NoSuchElementException |
if input is exhausted |
IllegalStateException |
if this scanner is closed |
short nextShort ()
扫描输入的下一个标记为 short 。
形式 nextShort()的这种方法调用的行为完全相同的方式调用 nextShort(radix),其中 radix是此扫描器的默认基数。
| 返回(Returns) | |
|---|---|
short |
the short scanned from the input |
| 抛出异常(Throws) | |
|---|---|
InputMismatchException |
if the next token does not match the Integer regular expression, or is out of range |
NoSuchElementException |
if input is exhausted |
IllegalStateException |
if this scanner is closed |
int radix ()
返回此扫描仪的默认基数。
扫描仪的基数会影响其默认编号与正则表达式匹配的元素; 见上面的localized numbers 。
| 返回(Returns) | |
|---|---|
int |
the default radix of this scanner |
void remove ()
此实现 Iterator不支持删除操作。
| 抛出异常(Throws) | |
|---|---|
UnsupportedOperationException |
if this method is invoked. |
也可以看看:
Scanner reset ()
重置此扫描仪。
重置扫描器会丢弃所有可能已经通过调用改变了明确的状态信息 useDelimiter(String) , useLocale(Locale) ,或 useRadix(int) 。
表单 scanner.reset()的此方法的调用的行为方式与调用完全相同
scanner.useDelimiter("\\p{javaWhitespace}+")
.useLocale(Locale.getDefault())
.useRadix(10);
| 返回(Returns) | |
|---|---|
Scanner |
this scanner |
Scanner skip (String pattern)
跳过与从指定字符串构造的模式相匹配的输入。
调用表单 skip(pattern)的此方法的行为与调用 skip(Pattern.compile(pattern))的行为完全相同。
| 参数(Parameters) | |
|---|---|
pattern |
String: a string specifying the pattern to skip over |
| 返回(Returns) | |
|---|---|
Scanner |
this scanner |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if this scanner is closed |
Scanner skip (Pattern pattern)
跳过与指定模式匹配的输入,忽略分隔符。 如果指定模式的锚定匹配成功,则此方法将跳过输入。
如果在当前位置找不到指定模式的匹配,则不会跳过输入并引发 NoSuchElementException 。
由于此方法试图匹配从扫描仪当前位置开始的指定图案,因此可以匹配大量输入(例如“。*”)的图案可能会导致扫描仪缓冲大量输入。
请注意,通过使用无法匹配的模式(例如, sc.skip("[ \t]*") ,可以跳过某些内容而不冒 NoSuchElementException的风险。
| 参数(Parameters) | |
|---|---|
pattern |
Pattern: a string specifying the pattern to skip over |
| 返回(Returns) | |
|---|---|
Scanner |
this scanner |
| 抛出异常(Throws) | |
|---|---|
NoSuchElementException |
if the specified pattern is not found |
IllegalStateException |
if this scanner is closed |
String toString ()
返回此Scanner的字符串表示Scanner 。 Scanner的字符串表示包含可能对调试有用的信息。 确切的格式是未指定的。
| 返回(Returns) | |
|---|---|
String |
The string representation of this scanner |
Scanner useDelimiter (Pattern pattern)
将此扫描仪的分隔模式设置为指定模式。
| 参数(Parameters) | |
|---|---|
pattern |
Pattern: A delimiting pattern |
| 返回(Returns) | |
|---|---|
Scanner |
this scanner |
Scanner useDelimiter (String pattern)
将此扫描仪的分隔图案设置为从指定的 String构造的图案。
调用表单 useDelimiter(pattern)的此方法的行为与调用 useDelimiter(Pattern.compile(pattern))的行为完全相同。
调用 reset()方法将扫描器的分隔符设置为 default 。
| 参数(Parameters) | |
|---|---|
pattern |
String: A string specifying a delimiting pattern |
| 返回(Returns) | |
|---|---|
Scanner |
this scanner |
Scanner useLocale (Locale locale)
将此扫描仪的语言环境设置为指定的语言环境。
扫描仪的语言环境会影响其默认基元匹配正则表达式的许多元素; 见上面的localized numbers 。
调用 reset()方法会将扫描仪的语言环境设置为 initial locale 。
| 参数(Parameters) | |
|---|---|
locale |
Locale: A string specifying the locale to use |
| 返回(Returns) | |
|---|---|
Scanner |
this scanner |
Scanner useRadix (int radix)
将此扫描仪的默认基数设置为指定的基数。
扫描仪的基数会影响其默认编号与正则表达式匹配的元素; 见上面的localized numbers 。
如果基数小于 Character.MIN_RADIX或大于 Character.MAX_RADIX ,则引发 IllegalArgumentException 。
调用 reset()方法将设置扫描仪的基数为 10 。
| 参数(Parameters) | |
|---|---|
radix |
int: The radix to use when scanning numbers |
| 返回(Returns) | |
|---|---|
Scanner |
this scanner |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if radix is out of range |