首页 > 解决方案 > 如何修复 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 中:

在此处输入图像描述

标签: javaandroid-studio

解决方案


尝试先分配给一个变量,

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


推荐阅读