Java.util.Scanner.next() 方法
描述
java.util.Scanner.next() 方法从这个扫描器中找到并返回下一个完整的令牌。 一个完整的标记前后是匹配分隔符模式的输入。 此方法可能会在等待输入扫描时阻塞,即使先前调用 hasNext() 返回 true。
声明
以下是 java.util.Scanner.next() 方法的声明
public String next()
参数
NA
返回值
该方法返回下一个令牌
异常
NoSuchElementException − 如果没有更多可用的令牌
IllegalStateException − 如果此扫描仪已关闭
示例
下面的例子展示了 java.util.Scanner.next() 方法的使用。
package com.tutorialspoint;
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!

