java.util.Scanner.hasNext() 方法如果此scanner有另一个标记在它的输入,则返回true。在等待要scanner的输入,此方法可能阻塞如果scanner不执行任何输入。
声明
以下是java.util.Scanner.hasNext()方法的声明
public boolean hasNext()
参数
-
NA
返回值
当且仅当此scanner有另一个标记,此方法返回true
异常
-
IllegalStateException -- 如果此scanner已关闭
例子
下面的示例演示java.util.Scanner.hasNext()方法的用法。
package com.yiibai; import java.util.*; import java.util.regex.Pattern; 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); // check if the scanner has a token System.out.println("" + scanner.hasNext()); // print the rest of the string System.out.println("" + scanner.nextLine()); // check if the scanner has a token after printing the line System.out.println("" + scanner.hasNext()); // close the scanner scanner.close(); } }
让我们来编译和运行上面的程序,这将产生以下结果:
true Hello World! 3+3.0=6 false