java - 如何修复 NoSuchElementException 扫描程序?
问题描述
Scanner
抛出 No line found 也是如此NoSuchElementException
,它不等待我的输入,请注意我只打电话nextLine()
我不打电话nextInt()
无处。我只是打电话nextLine()
,只有一次。
这是我所有的代码
public class MyClass{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println(scanner.nextLine());
}
}
这是我得到的错误......
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at com.ekisache.games.MyClass.main(MyClass.java:9)
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':Test:MyClass.main()'.
> Process 'command 'C:/Program Files/Android/Android Studio/jre/bin/java.exe'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
这很奇怪,因为在我的另一台电脑上它可以工作,而在这台电脑上却不行
我的mac中的工作代码:
在我的 Windows PC 中:
解决方案
尝试先分配给一个变量,
String yourInput = scanner.nextLine();
System.out.printLn(yourInput)
推荐阅读
- ios - XCode 构建错误:架构 x86_64 的未定义符号:“_FIREmailAuthProviderID”
- google-apps-script - Google Docs API - 完整文档(超链接问题)
- c# - 外部样式表不适用于 HtmlTextWriter
- linux - 在linux上构建时如何获取wireshark可执行文件
- javascript - 添加子视图时,React Native 父视图大小正在发生变化
- java - 在 Windows 7 中报告了缺少依赖项 POM,但在 Windows 10 中没有
- android - getActivity() 在处理程序中为 null
- spring - Spring Boot + Activiti
- c++ - 如何处理:“重复连接...”和“计时器不能从另一个线程停止...”
- javascript - 在 React 中正确创建全局变量