Java.util.Scanner.next() 方法
描述
java.util.Scanner.next(String pattern) 方法返回下一个标记,如果它与从指定字符串构造的模式匹配。 如果匹配成功,扫描器会前进超过匹配模式的输入。
声明
以下是 java.util.Scanner.next() 方法的声明
public String next(String pattern)
参数
pattern − 指定要扫描的模式的字符串
返回值
此方法返回下一个标记
异常
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);
// check if next token matches the pattern and print it
System.out.println("" + scanner.next("Hello"));
// check if next token matches the pattern and print it
System.out.println("" + scanner.next("World!"));
// close the scanner
scanner.close();
}
}
让我们编译并运行上面的程序,这将产生以下结果 −
Hello World!

