java.util.Scanner.hasNext(Pattern pattern)%uA0如果下一个完整标记与指定模式匹配方法返回true。一个完整的标记前缀和后缀由输入相匹配的分隔符的模式。在等待输入此方法可能阻塞。scanner不执行任何输入。
声明
以下是java.util.Scanner.hasNext()方法的声明
public boolean hasNext(Pattern pattern)
参数
-
pattern%uA0--%uA0要扫描的模式
返回值
当且仅当此scanner 有另一个标记与指定模式匹配此方法返回true
异常
-
IllegalStateException%uA0-- 如果此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&aposs next token matches "rld" following 2 chars System.out.println("" + scanner.hasNext(Pattern.compile("..rld"))) // check if the scanner&aposs next token matches "llo" following 2 chars System.out.println("" + scanner.hasNext(Pattern.compile("..llo"))) // print the rest of the string System.out.println("" + scanner.nextLine()) // close the scanner scanner.close() } }
让我们来编译和运行上面的程序,这将产生以下结果:
false true Hello World! 3+3.0=6