【问题标题】:How to fix NoSuchElementException Scanner?如何修复 NoSuchElementException 扫描程序?
【发布时间】:2020-04-02 00:02:43
【问题描述】:

所以Scanner 正在抛出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 电脑中:

【问题讨论】:

  • 该代码在哪个上下文中执行?从控制台?来自 IDE?
  • @Themelis 来自 IDE, android studio
  • System.in 对应的 Standard Input 通常是来自键盘的输入(System.out 通常是控制台)但也许 Android Studio 正在改变这一点.在您的另一台计算机上运行的代码是否也在 Android Studio 中运行?
  • 代码没问题,我的意思是你不会出错 -> Scanner scanner = new Scanner(System.in); System.out.println(scanner.nextLine());
  • @Themelis 是的,相同的代码,相同的 IDE

标签: java android-studio


【解决方案1】:

先尝试赋值给一个变量,

String yourInput = scanner.nextLine();
System.out.printLn(yourInput)

【讨论】:

    猜你喜欢
    • 2015-09-06
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多