java - NoSuchElementException:使用 hasNextLine 找不到行
问题描述
我已经包含了一个 hasNextLine 异常,但我仍然收到 NoSuchElementException 错误
我正在创建一个从文件扫描的回文程序。我可以让它从文件中扫描,但它只返回非回文词,并返回 NoSuchElementException: No Line Found 的错误,即使我已经包含了一个 hasNextLine。
case 3:
System.out.println("Palindrome problem.");
while (input.hasNextLine()) {
String text, reverse = input.nextLine();
text = input.nextLine();
int length = text.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + text.charAt(i);
if (text.equals(reverse))
System.out.println(text + "is a palindrome.");
else
System.out.println(text + "isn't a palindrome.");
}
break;
我希望代码读取整个文件并返回回文和非回文词
解决方案
这是因为您在input.nextLine()
这里调用了两次:
String text, reverse = input.nextLine();
text = input.nextLine();
执行时text = input.nextLine();
,该行已被消耗。
推荐阅读
- django - 如何让用户关联到另一个模型 DRF。ValueError:精确查找的 QuerySet 值必须使用切片限制为一个结果
- mysql - 无法在 M1 MacBook Big Sur 中安装 mysql2 0.5.3
- django - 如何在不使用 for 循环的情况下解决这个 django 查询问题?
- c# - 如何通过反射清除类中的所有静态事件处理程序?
- django - Django 的无限流响应在 apache 日志中记录 500
- python - Python:subprocess.call 和变体对于特定应用程序从执行的 .py 中失败,但不是从 CLI 中的 python
- google-cloud-firestore - Firestore 聚合 - 扇出和更新云功能
- bash - bash parse options from string created in other method
- node.js - 嘿,我正在尝试与 android、nodejs、mysql db 进行一对一聊天
- excel - Excel 2016 标准另存为 csv utf-8 缺失