boolean hasNextInt(int radix)
描述 (Description)
如果此扫描器输入中的下一个标记可以使用nextInt()方法解释为指定基数中的int值,则java.util.Scanner.hasNextInt(int radix)方法返回true。 扫描仪不会超过任何输入。
声明 (Declaration)
以下是java.util.Scanner.hasNextInt()方法的声明public boolean hasNextInt(int radix)
参数 (Parameters)
radix - 用于将标记解释为int值的基数
返回值 (Return Value)
当且仅当此扫描器的下一个标记是有效的int值时,此方法才返回true
异常 (Exception)
IllegalStateException - 如果此扫描程序已关闭
例子 (Example)
以下示例显示了java.util.Scanner.hasNextInt()方法的用法。package com.iowiki;
import java.util.*;
public class ScannerDemo {
public static void main(String[] args) {
String s = "Hello World! 3 + 3.0 = 6 ";
// create a new scanner with the specified String Object
Scanner scanner = new Scanner(s);
while (scanner.hasNext()) {
// check if the scanner's next token is an int with a radix of 4
System.out.println("" + scanner.hasNextInt(4));
// print what is scanned
System.out.println("" + scanner.next());
}
// close the scanner
scanner.close();
}
}
让我们编译并运行上面的程序,这将产生以下结果 -false
Hello
false
World!
true
3
false
+
false
3.0
false
=
false
6