Home > OS >  NoSuchElementException Problem in User Input Java
NoSuchElementException Problem in User Input Java

Time:05-24

I'm confused while using an Java program I created.

public static void main(String[] args) {
    Scanner scanner1 = new Scanner(System.in);
    int input1 = 0;
    boolean Input1Real = false;
    System.out.print("Your first input integer? ");
    while (!Input1Real) {
        String line = scanner1.nextLine();
        try {
            input1 = Integer.parseInt(line);
            Input1Real = true;
        }
        catch (NumberFormatException e) {
            System.out.println("Use an integer! Try again!");
            System.out.print("Your first input integer? ");
        }
    }
    System.out.println("Your first input is "   input1);
}

Initially, when a user Ctrl D during the input, it will promptly end the program and display an error in the form of this,

    Your first input integer? ^D
    Class transformation time: 0.0073103s for 244 classes or 2.9960245901639343E-5s per class
    Exception in thread "main" java.util.NoSuchElementException: No line found
        at java.base/java.util.Scanner.nextLine(Scanner.java:1651);
        at Playground.Test1.main(Test1.java:13)

Doing a bit of research I note that Ctrl D terminates the input of sort. Therefore, I tried add few more lines to my codes to prevent the error from appearing again and instead printing a simple "Console has been terminated successfully!" and as far as my skills can go.

public static void main(String[] args) {
    Scanner scanner1 = new Scanner(System.in);
    int input1 = 0;
    boolean Input1Real = false;
    System.out.print("Your first input integer? ");
    while (!Input1Real) {
        String line = scanner1.nextLine();
        try {
            try {
                input1 = Integer.parseInt(line);
                Input1Real = true;
            }
            catch (NumberFormatException e) {
                System.out.println("Use an integer! Try again!");
                System.out.print("Your first input integer? ");
            }
        }
        catch (NoSuchElementException e) {
            System.out.println("Console has been terminated successfully!");
        }
    }
    System.out.println("Your first input is "   input1);
}

In the end, I still got the same error.

CodePudding user response:

Got it!, the code hasNext() will ensure that the error will not appear. This method is to check whether there is another line in the input of the scanner and to check if its filled or empty. I am also using null to check my statement after passing the loop so the program stops if the input value is still null while keeping the function of Ctrl D.

public static void main(String[] args) {
    Integer input1 = null;
    System.out.println("Your first input integer? ");
    Scanner scanner1 = new Scanner(System.in);
    while(scanner1.hasNextLine()) {
        String line = scanner1.nextLine();
        try {
            input1 = Integer.parseInt(line);
            break;
        }
        catch (NumberFormatException e) {
            System.out.println("Use an integer! Try again!");
            System.out.println("Your first input integer? ");
        }
    }
    if (input1 == null) {
        System.out.println("Console has been terminated successfully!");
        System.exit(0);
    }
    System.out.println(input1);
}

This solution is not prefect of course but I would appreciate if there were much simpler options.

  • Related