Как исправить сканер NoSuchElementException? - PullRequest
2 голосов
/ 02 апреля 2020

Итак, 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

Это довольно странно, как в на моем другом компьютере это работает, а на этом нет

Рабочий код в моей ма c:

enter image description here

По моему windows P C:

enter image description here

1 Ответ

0 голосов
/ 02 апреля 2020

Попробуйте сначала назначить переменную,

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...