问题
I want to take an input from user with using Scanner in my project. It gives same error so i created another project to try Scanner only but it still gives this error.
I looked up for solved questions in stackoverflow but they're large codes than mine, so maybe NoSuchElementException caused by their codes. I mean isn't it weird in simple Scanner code? Simpler codes are using in Scanner tutorials. Am i doing little thing wrong? Here it is:
package deneme;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number1 = input.nextInt();
}
}
The NetBeans output is:
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
cd /home/frogwine/NetBeansProjects/deneme; /home/frogwine/.gradle/wrapper/dists/gradle-4.10.2-bin/cghg6c4gf4vkiutgsab8yrnwv/gradle-4.10.2/bin/gradle --configure-on-demand -x check run
Configuration on demand is an incubating feature.
> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :run FAILED
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at deneme.Main.main(Main.java:20)
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':run'.
> Process 'command '/usr/lib/jvm/java-11-openjdk-amd64/bin/java'' 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
BUILD FAILED in 1s
2 actionable tasks: 1 executed, 1 up-to-date
My NetBeans informations are:
Product Version: Apache NetBeans IDE 11.0 (Build incubator-netbeans-release-404-on-20190319)
Updates: Updates available
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-6-amd64 running on amd64; UTF-8; en_US (nb)
User directory: /home/frogwine/.netbeans/11.0
Cache directory: /home/frogwine/.cache/netbeans/11.0
回答1:
I can reproduce your problem on NetBeans 11.1 Unfortunately this is a known issue. See these NetBeans Bug Reports:
- NETBEANS-2832 Support System input into Gradle output window.
- NETBEANS-3073 Can't get Console input with Gradle project
Both of those bugs are marked with Status: RESOLVED
because a fix for this is issue is to be included in the next release of NetBeans, version 11.2, which is tentatively scheduled for release later this month.
If you need an immediate solution, there are a couple of workarounds:
Run your application from the command line instead of within NetBeans. As you noted in a comment to your own question, that works fine. So there is clearly nothing wrong with your code or NetBeans; the issue lies with the implementation of Gradle support for program input within NetBeans. The failed output shows the commands to be run from the command line, so just copy and paste them for convenience.
Download and install a beta version of NetBeans 11.2. For example, file netbeans-11.2-beta2-bin.zip from here. I tried that and everything worked fine when inputting the number 54321, then displaying the value that was read within NetBeans:
JAVA_HOME="C:\Java\jdk-11" cd D:\NB112\gradleproject1; C:\Users\johndoe.gradle\wrapper\dists\gradle-4.10.2- bin\cghg6c4gf4vkiutgsab8yrnwv\gradle-4.10.2\bin\gradle --configure-on-demand -x check run Configuration on demand is an incubating feature.
Task :compileJava UP-TO-DATE Task :processResources NO-SOURCE Task :classes UP-TO-DATE 54321
Task :run number1=54321
BUILD SUCCESSFUL in 13s 2 actionable tasks: 1 executed, 1 up-to-date
As a separate but related matter, note that you also need to include the following in your build.gradle file:
run{
standardInput = System.in
}
So be sure to add that your project's build.gradle if it is not there already. See this SO answer for more information on that.
来源:https://stackoverflow.com/questions/58259574/java-util-nosuchelementexception-in-basic-scanner-usage