0

I've created a java project in IDE Netbeans 12 ( java with Gradle ). And when I was trying to run a simple "get input" code as below:

package hi;
import java.util.Scanner;

public class Main {
    public static void main( String args[] ){
        Scanner scan = new Scanner( System.in );
        int number   = scan.nextInt();
        System.out.print( number );
    }
}

I got an error:

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 hi.Main.main(Main.java:7)

I used try catch to determine the error occurred on the line int number = scan.nextInt();. It just happened when building Java project with Gradle, others are fine. I tried to find out on Google search but it looks like I have not found any solution or the reason of the error. Anyone here helppppp

  • "_I used try catch to determine the error occurred on the line `int number = scan.nextInt();`_" – In the hopes of making debugging easier for you in the future, you don't have to randomly insert try-catch blocks in your code to determine which exact line threw the exception. If you look at the stack trace you'll see `at hi.Main.main(Main.java:7)`. That line tells you the exception was thrown on line `7` of the `hi.Main` class, in a method named `main`. See [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/questions/3988788) for more details – Slaw Aug 15 '20 at 04:59
  • I recommend using a cleaner IDE about gradle controls, try Intellij IDEA https://www.jetbrains.com/idea/download/ There is a community edition. – Otid0 Aug 15 '20 at 18:28

0 Answers0