为什么当我没有机会输入时扫描仪会抛出异常? [重复]

问题描述 投票:0回答:1

我正在尝试编写一个类来收集和存储用户输入的方程的边界。但是,我的扫描仪不断抛出异常,不允许我输入任何内容。

这是我的代码:

public void setRange() {

        this.lowerBound = 0;
        this.upperBound = 0;
        this.numberOfIntervals = 0;
        boolean validInput = false;

        Scanner input = new Scanner(System.in);

        while (!validInput) {
            try {
                System.out.println("Type in the range: ");
                System.out.print("Lower Bound (double): ");
                lowerBound = input.nextDouble();

                System.out.print("Upper bound (double): ");
                upperBound = input.nextDouble();
                if (lowerBound > upperBound) {
                    throw new Exception();
                }

                System.out.print("Number of Intervals (int): ");
                numberOfIntervals = input.nextInt();
                if (numberOfIntervals <= 0) {
                    throw new Exception();
                }

                validInput = true;

            } catch (Exception ex) {

                System.out.println("Invalid input, please try again. ");
                input.nextLine();

            } // try-catch
        } // while loop
    } // setRange() method

我收到以下错误:

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
    at PolynomialUI.setRange(PolynomialUI.java:135)
    at PolynomialUI.runPolynomial(PolynomialUI.java:50)
    at PolynomialUI.<init>(PolynomialUI.java:28)
    at PolynomialUI.main(PolynomialUI.java:21)

第 135 行是我将扫描器推进到 catch 语句中的下一行的地方。我尝试将其注释掉,并意识到异常实际上来自于 try 块开始时第一次调用 input.nextDouble() 。

我尝试注释掉 nextDouble() 调用,将扫描器声明移到 try 块内,并完全注释掉整个 try-catch 块。如果我去掉 input.nextLine() 调用,我只会得到一个无限循环。

非常感谢任何帮助。这是我的第一个问题帖子,所以如果我犯了任何错误,请告诉我。

java exception java.util.scanner
1个回答
0
投票

您面临的问题是

nextDouble()
方法仅读取双精度值,并将换行符(Enter 键)留在输入缓冲区中。当您在 catch 块中调用
nextLine()
时,它会消耗该换行符,从而产生
NoSuchElementException

要解决此问题,您可以在每次调用

input.nextLine()
nextDouble()
后立即添加
nextInt()
以使用换行符。

这是修改后的代码:

public void setRange() {
    this.lowerBound = 0;
    this.upperBound = 0;
    this.numberOfIntervals = 0;
    boolean validInput = false;

   Scanner input = new Scanner(System.in);

    while (!validInput) {
        try {
            System.out.println("Type in the range: ");
            System.out.print("Lower Bound (double): ");
            lowerBound = input.nextDouble();
            input.nextLine(); // Consume the newline character

            System.out.print("Upper bound (double): ");
            upperBound = input.nextDouble();
            input.nextLine(); // Consume the newline character

            if (lowerBound > upperBound) {
                throw new Exception();
            }

            System.out.print("Number of Intervals (int): ");
            numberOfIntervals = input.nextInt();
            input.nextLine(); // Consume the newline character

            if (numberOfIntervals <= 0) {
                throw new Exception();
            }

            validInput = true;

        } catch (Exception ex) {
            System.out.println("Invalid input, please try again.");
            input.nextLine(); // Consume the newline character
        }
    }

    // Close the scanner to avoid resource leak
    input.close();
}

此修改可确保在每次

nextDouble()
nextInt()
调用后消耗换行符,从而防止
NoSuchElementException
问题。此外,我在末尾添加了
input.close()
以关闭扫描仪并避免潜在的资源泄漏。

© www.soinside.com 2019 - 2024. All rights reserved.