Reputation: 11
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?
Upvotes: 1
Views: 941
Reputation: 11
Just write
run.standardInput = System.in;
In your gradle.build file
Upvotes: 1