How to fix NoSuchElementException Scanner?

Viewed 625

So Scanner is throwing NoSuchElementException No line found, its like, its not waiting for my input, note that I only call nextLine() I don't call nextInt() nowhere. I just call nextLine() and only once.

This all my code

public class MyClass{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.println(scanner.nextLine());
    }
}

This is the error I'm getting...

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

Its pretty weird as in my other computer this works, and in this one it doesn't

Working code in my mac:

enter image description here

In my windows PC:

enter image description here

1 Answers

Try assigning to a variable first,

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

Related