java.util.Scanner.next() 方法查找并返回来自此scanner的下一个完整标记。一个完整的令牌之前和之后的输入相匹配的分隔符的模式。在等待要扫描的输入此方法可能阻塞,即使hasNext()以前调用返回true。
声明
以下是java.util.Scanner.next()方法的声明
public String next()
参数
-
NA
返回值
此方法返回下一个标记
异常
-
NoSuchElementException -- 如果没有更多的令牌可用
-
IllegalStateException -- 如果此scanner 已关闭
例子
下面的示例演示java.util.Scanner.next()方法的用法。
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); // find the next token and print it System.out.println("" + scanner.next()); // find the next token and print it System.out.println("" + scanner.next()); // close the scanner scanner.close(); } }
让我们来编译和运行上面的程序,这将产生以下结果:
Hello World!