1

I am trying to read input from the console using the Scanner class but no matter what I've tried so far it always errors on my computer with

"Exception in thread "main" java.util.NoSuchElementException: No line found"

public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);
    System.out.println("Enter username");
    String userName = myObj.nextLine();
    System.out.println("Username is: " + userName);
}

I tried this with an online code site Repl.it and it works fine there but not on my computer. I am running Debian Buster with the latest OpenJDK and JRE with the latest version NetBeans IDE.

This is my system info from NetBeans

Product Version: Apache NetBeans IDE 11.1
Java: 11.0.4; OpenJDK 64-Bit Server VM 11.0.4+11-post-Debian-1deb10u1
Runtime: OpenJDK Runtime Environment 11.0.4+11-post-Debian-1deb10u1
System: Linux version 4.19.0-5-amd64 running on amd64; ANSI_X3.4-1968;        en_US (nb)

I have tried checking for the next element using hasNext() like this

public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);
    System.out.println("Enter username");
    if(myObj.hasNext()){
        String userName = myObj.nextLine();
        System.out.println("Username is: " + userName);
    }
}

The console never waits for user input so hasNext() always returns false. This code works okay on repl.it but not on my computer. I have tried purging and reinstalling the JDK, JRE, and reinstalling NetBeans but the issue persists and I'm not sure what the issue is now.

How can I fix this problem?

Javinus
  • 11
  • 1
  • Are you launching your application via a build tool such as Gradle? – Slaw Aug 31 '19 at 09:57
  • yes, creating a new java project using Gradle in NetBeans Edit: I just tried it using creating a Maven project and the code works perfectly. I wonder why Gradle has a problem with it? – Javinus Aug 31 '19 at 10:01
  • 2
    Possible duplicate of [java.util.scanner throws NoSuchElementException when application is started with gradle run](https://stackoverflow.com/questions/36723447/java-util-scanner-throws-nosuchelementexception-when-application-is-started-with) – Slaw Aug 31 '19 at 10:08
  • Yes that looks relevant. I couldn't get the fix in that question to work though so I'm just going to use Maven instead as that one worked. Thanks for helping. – Javinus Aug 31 '19 at 10:35
  • Interesting. A deleted answer also says they couldn't get it working on NetBeans 11, but it gave no other solutions (hence why it was deleted, it was not an answer). I wonder what's different about NetBeans. When you tried the fix do you just get the same `NoSuchElementException` thrown or does some other error occur? – Slaw Aug 31 '19 at 10:51
  • Yes I just get the same NoSuchElementException error. There isn't any different errors between before and after trying that fix. – Javinus Aug 31 '19 at 11:15
  • Please enter the full stacktrace. No java debug question can be easily answered without a full stacktrace. – peterh Aug 31 '19 at 13:12
  • I've saved a full stacktrace here https://pastebin.com/f8cG2Cf2 – Javinus Sep 01 '19 at 01:27

1 Answers1

0

Just write

run.standardInput = System.in;

In your gradle.build file