
Java Scanner 类的 next() 方法及其变体(如 nextInt())用于从输入流中读取“下一个令牌”。这些方法首先会跳过与定界符模式匹配的输入(默认是空白字符),然后尝试返回紧随其后的有效数据单元。理解“next”的含义关键在于将其视为“下一个令牌”,而非“下一行”,这对于正确处理用户输入至关重要。
java.util.Scanner 是 Java 中一个功能强大的类,主要用于解析原始类型和字符串的文本输入。它能够从各种来源读取数据,例如 System.in(标准输入)、文件、字符串等。Scanner 的核心功能在于其能够将输入分解为一系列“令牌”(tokens),并根据这些令牌的类型进行处理。
当我们在 Scanner 中看到 next()、nextInt()、nextDouble() 等方法时,这里的“next”并非指“下一行”,而是指“下一个令牌”。
next() 方法的工作原理可以概括为以下两步:
立即学习“Java免费学习笔记(深入)”;
例如,如果输入是 " hello world",当调用 scanner.next() 时:
理解 next() 系列方法与 nextLine() 的区别是使用 Scanner 的关键。
常见陷阱: 当在一个 next() 或 nextInt() 调用之后紧接着调用 nextLine() 时,可能会出现问题。因为 next() 或 nextInt() 没有消耗掉行末的换行符,所以 nextLine() 会立即读取到这个残留的换行符,并返回一个空字符串。
解决方案: 在 next() 或 nextInt() 之后调用 nextLine() 之前,通常需要额外调用一次 scanner.nextLine(); 来消耗掉之前未处理的换行符。
下面通过一个示例来演示 Scanner 中 next()、nextInt() 和 nextLine() 的行为。
import java.util.Scanner;
public class ScannerNextDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("--- 演示 next() 和 nextInt() ---");
System.out.println("请输入多个单词和数字,用空格分隔,然后按回车 (例如: Java 101 Programming 2023):");
// 假设用户输入: Java 101 Programming 2023
// 使用 hasNext() 检查是否有下一个令牌,避免 NoSuchElementException
if (scanner.hasNext()) {
String word1 = scanner.next(); // 读取 "Java"
System.out.println("第一个令牌 (String): " + word1);
}
if (scanner.hasNextInt()) {
int number1 = scanner.nextInt(); // 读取 "101" 并转换为 int
System.out.println("第二个令牌 (int): " + number1);
}
if (scanner.hasNext()) {
String word2 = scanner.next(); // 读取 "Programming"
System.out.println("第三个令牌 (String): " + word2);
}
if (scanner.hasNextInt()) {
int number2 = scanner.nextInt(); // 读取 "2023" 并转换为 int
System.out.println("第四个令牌 (int): " + number2);
}
// --- 演示 nextLine() ---
System.out.println("\n--- 演示 nextLine() (处理残留换行符) ---");
System.out.println("请输入一行文本 (可能包含空格), 然后按回车 (例如: This is a complete line.):");
// !!! 注意: 在 nextInt() 之后,行末的换行符仍然在输入缓冲区中。
// 如果不先消耗掉它,接下来的 nextLine() 会直接读取到这个空行。
scanner.nextLine(); // 消耗掉之前 nextInt() 遗留的换行符
String fullLine = scanner.nextLine(); // 读取整行 "This is a complete line."
System.out.println("读取的整行文本: " + fullLine);
// --- 演示自定义定界符 ---
System.out.println("\n--- 演示自定义定界符 ---");
System.out.println("请输入用逗号分隔的列表 (例如: apple,banana,cherry):");
scanner.nextLine(); // 再次消耗掉上一个 nextLine() 留下的换行符
String commaSeparatedInput = scanner.nextLine(); // 读取 "apple,banana,cherry"
Scanner customScanner = new Scanner(commaSeparatedInput);
customScanner.useDelimiter(","); // 将逗号设为定界符
while (customScanner.hasNext()) {
System.out.println("自定义定界符解析的令牌: " + customScanner.next());
}
customScanner.close();
scanner.close(); // 关闭 Scanner 对象,释放资源
}
}通过深入理解 Scanner 的工作机制,特别是“令牌”和“定界符”的概念,以及 next() 和 nextLine() 之间的区别,可以更有效地处理各种用户输入,并编写出健壮的 Java 应用程序。
以上就是深入理解 Java Scanner 的 next() 方法与令牌解析的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号