1

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
skomisa
  • 16,436
  • 7
  • 61
  • 102
  • are you sure you're running this particular `main` ? are there any other `main`s in the project or projects? – mangusta Oct 06 '19 at 17:32
  • @mangusta yes, i also closed my actually working project. and there is no another main code blocks. –  Oct 06 '19 at 17:34
  • What is the value you are giving while running the code?can you add the full stack trace of the error you are getting? – Sujay Mohan Oct 06 '19 at 17:37
  • @SujayMohan whatever the value is, it shouldn't give nosuchelementexception – mangusta Oct 06 '19 at 17:38
  • @SujayMohan nothing, the program can't start so i can't provide a value. –  Oct 06 '19 at 17:39
  • @frogwine what's the structure of your project? the code has no flaws, probably something runs prior to invoking `main` – mangusta Oct 06 '19 at 17:39
  • You are using nextInt() and not nextLine(). So it matters – Sujay Mohan Oct 06 '19 at 17:39
  • @SujayMohan i know nextLine() is for string input, i want to take integer input, so i used nextInt() –  Oct 06 '19 at 17:42
  • Please post the full error stack trace. – Sujay Mohan Oct 06 '19 at 17:42
  • Have you tried outside netbeans? – Sujay Mohan Oct 06 '19 at 17:43
  • Sorry for that, i forgot adding error outputs, i edited the post. Could you take a look again? and my project tree is: https://i.imgur.com/tyS23hM.png –  Oct 06 '19 at 17:47
  • you're using `java-11-openjdk-amd64`. downgrade to the lower version and re-check – mangusta Oct 06 '19 at 17:52
  • javac can compile without trouble and worked with java terminal program. It looks like NetBeans' problem. Thanks a lot. –  Oct 06 '19 at 18:00
  • 1
    I would vote to close this question as a duplicate of [java.util.scanner throws NoSuchElementException when application is started with gradle run](https://stackoverflow.com/questions/36723447/) since a major part of the problem is due to how Gradle launches the application, but I've seen multiple people say the solution provided doesn't work with NetBeans 11. However, you can try to see if the solution works for you. – Slaw Oct 06 '19 at 18:02
  • @Slaw The link you provided is important and relevant, but this is not a duplicate of that question. The larger problem is a bug which will be fixed in NetBeans 11.2 - see my answer below. – skomisa Oct 09 '19 at 18:02
  • @skomisa Hence the reason why I didn't vote to close as a duplicate and only said "_I would vote to close [...] but [...]_". That said, thanks for providing an excellent answer. +1 – Slaw Oct 09 '19 at 18:23
  • @Slaw Uh-oh .... I completely misread/misinterpreted your comment. My fault completely, and my apologies. – skomisa Oct 09 '19 at 18:35
  • @skomisa No worries and no harm done. – Slaw Oct 09 '19 at 18:39

1 Answers1

1

I can reproduce your problem on NetBeans 11.1 Unfortunately this is a known issue. See these NetBeans Bug Reports:

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:

  1. 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.

  2. 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.

skomisa
  • 16,436
  • 7
  • 61
  • 102