Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

Same Java code Failed to run under Java Gradle Application

I have come across an issue when running the following code under Netbeans Gradle Java Application.

import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class Main {
    private static int imgBundle;
    private static int flacBundle;
    private static int vidBundle;
    public static void main(String[] args) {
        takeInput();
    }
    private static void takeInput() {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter the number of Image format for the order: ");
        imgBundle = input.nextInt();     // where the error occurred
        System.out.print("Please enter the number of Audio format for the order: ");
        flacBundle = input.nextInt();    // where the error occurred
        System.out.print("Please enter the number of Video format for the order: ");
        vidBundle = input.nextInt();     // where the error occurred
        System.out.println("Your Order Input:");
        System.out.println(imgBundle + " IMG");
        System.out.println(flacBundle + " FLAC");
        System.out.println(vidBundle + " VID");
        System.out.println("");
        System.out.println("Calculating...Please wait...");
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
    }
}

An Error occurred

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 BundlesCalculator.Main.takeInput(Main.java:65)
    at BundlesCalculator.Main.main(Main.java:43)

However, I wrote the same code in a simple Java Application without Gradle, there is no issue occurred. I just wonder what did I do incorrectly?

question from:https://stackoverflow.com/questions/65839373/same-java-code-failed-to-run-under-java-gradle-application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Scanner will throw that exception if there is no more input to be read. This can happen if your input file is empty, or it doesn't contain enough data for all the nextInt() calls.

If input is from the terminal, you provided end-of-file (Ctrl-Z on Windows, Ctrl-D on Linux) before all the input was read.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...