-4

Recently I face a strange problem in java program. I need to take user input and need to do some calculation on it. However when I am running a program with user input using Scanner class, I got same error message. I have tried using NetBeans 12.0 with Gradle and other online java compiler to see the problem. But in both cases I got the same exception error message. As in every program with user input with Scanner class, I got the same error message, hence, as a reference I am giving a sample program below, which also gives the same error message.

package RevisedMixedAlgoFindMaxPairWiseProduct;

import java.util.Scanner;

public class RevisedMixedAlgoFindMaxPairWiseProduct {

    public static long array[];

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        System.out.println("num:" + num);
    }
}


    

When I compile, it shows no error. But at the time of running the program, it shows the error messages, shown in below.The message is(Netbeans's error message):

JAVA_HOME="C:\Program Files\Java\jdk-15"
cd C:\Users\Rasel Azam\Documents\NetBeansProjects\RevisedMixedAlgoFindMaxPairWiseProduct; C:\Users\Rasel Azam\.gradle\wrapper\dists\gradle-6.3-bin\8tpu6egwsccjzp10c1jckl0rx\gradle-6.3\bin\gradle --configure-on-demand -x check run
Configuration on demand is an incubating feature.
> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes

> 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 RevisedMixedAlgoFindMaxPairWiseProduct.RevisedMixedAlgoFindMaxPairWiseProduct.main(RevisedMixedAlgoFindMaxPairWiseProduct.java:28)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command 'C:\Program Files\Java\jdk-15\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 2s
2 actionable tasks: 2 executed

But the same program is successfully run in Windows command prompt and BlueJ IDE.

I could not find what is the problem is [but while running the program, I got input buffer limit in a condition in Scanner class,is there any input buffer limit in memory in RAM] can you suggest me how can I solve this problem. For your information My PC configuration is

8GB RAM Intel(R) Core(TM) i3-3110M CPU @ 2.40GHz 2.40 GHz Windows 10 Pro HP Probook 450 GO

  • Well, when I copied this code into an IDE, it started complaining about duplicate class, probably because the package name is the same as the class name. You should at least change the package to a different name. – MC Emperor Mar 29 '21 at 14:32
  • 1
    @MCEmperor thanks for your comment. I have changed it and still the problem exists. But I have found the problem and got the solution from another post of stack overflow and gradle discussion group.For user input, I should add run{ standardInput = System.in } in build.gradle file. – Rasel Azam Mar 29 '21 at 14:55

1 Answers1

0

Probably the problem with the settings with Gradle and some version of JDK.

Though I have been facing the problem when I running a program with user input in NetBeans 12.0 version with Gradle. And it also shows same error for some online java compiler. Don't know why does it happen, is it my PC's problem or not?

But the same program has successfully run when I create a project in NetBeans with Ant. And it is also successfully run in some other online compilers.