java.util.Scanner.hasNextInt(int radix) 如果在此scanner 输入信息中的下一个标记可以使用nextInt()方法被解释为一个int值指定基数,方法返回true。scanner 不执行任何输入。
声明
以下是java.util.Scanner.hasNextInt()方法的声明
public boolean hasNextInt(int radix)
参数
-
radix -- 基数用于将标记解释为一个int值
返回值
当且仅当此scanner的下一个标记是有效的int值,此方法返回true
异常
-
IllegalStateException -- 如果此scanner 已关闭
例子
下面的示例演示java.util.Scanner.hasNextInt()方法的用法。
package com.yiibai; 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