Java.util.Scanner.hasNext() 方法
描述
java.util.Scanner.hasNext() 方法 如果此扫描器的输入中有另一个标记,则返回 true。 此方法可能会在等待输入扫描时阻塞。 扫描仪不会超过任何输入。
声明
以下是 java.util.Scanner.hasNext() 方法的声明
public boolean hasNext()
参数
NA
返回值
当且仅当此扫描器有另一个令牌时,此方法才返回 true
异常
IllegalStateException − 如果此扫描仪已关闭
示例
下面的例子展示了 java.util.Scanner.hasNext() 方法的使用。
package com.tutorialspoint;
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

