When I try to run my java project in NetBeans with a Scanner in it, it doesn't run and shows error message in the output field. "FAILURE: Build failed with an exception.(Scanner). processResources NO-SOURCE".
When I delete names[x] = s.next();
and grades[x] = s.nextInt();
it runs. I looked for a solution a lot but non of them have worked for me. Note that I can run the same code in Android Studio. I'm a beginner in programming, anyone have an idea how to fix it??
My code:
package codePracticing;
public class Main {
public static void main(String args[]) {
java.util.Scanner s = new java.util.Scanner(System.in);
String names[] = new String[5];
int grades[] = new int[5];
for (int x = 0; x < names.length; x++){
if (x != 3){
System.out.println("Enter Name Of Student Number" + String.valueOf(x + 1) + ": ");
names[x] = s.next();
System.out.println("Enter Grade Of Student Number" + String.valueOf(x + 1) + ": ");
grades[x] = s.nextInt();
}
}
for (int x = 0; x < names.length; x++){
if (x != 3){
System.out.println("Student Number " + String.valueOf(x + 1) + " " + names[x] + " ; Grade: " + grades[x]);
}
}
The error message in the Output tab:
JAVA_HOME="C:\Program Files\Java\jdk-16.0.2"
cd C:\Users\***\Documents\NetBeansProjects\codePracticing; .\gradlew.bat --configure-on-demand -x check run
Configuration on demand is an incubating feature.
> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :run FAILED
Enter Name Of Student Number1:
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:1478)
at codePracticing.Main.main(Main.java:28)
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':run'.
> Process 'command 'C:\Program Files\Java\jdk-16.0.2\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
BUILD FAILED in 381ms
2 actionable tasks: 2 executed
Thanks..
I tried to copy the code to a "Java with Ant" project (instead of "Java with Gradle") and it worked without problems.
File → New project → Java with Ant → Java Application
There is no problem in this code if you run it on java/ant. but if you want to run it on gradle you should see this post. java.util.scanner throws NoSuchElementException when application is started with gradle run
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.