while循环解析Double.IsNaN不正确

时间:2022-10-30 00:22:50

Language: Java, IDE: eclipse mars

语言:Java,IDE:eclipse mars

The program is supposed to prompt the user (using JOptionPane) for a positive value. I'm trying to catch the invalid entries. My while statement catches the negative numbers but not the strings. When a negative number is entered, the prompt is shown again, but when a string value is entered, the exception is caught and the program moves on (when it should re prompt the user).

该程序应该提示用户(使用JOptionPane)获得正值。我正试图抓住无效的条目。我的while语句捕获负数而不是字符串。输入负数时,会再次显示提示,但是当输入字符串值时,将捕获异常并继续执行程序(当它应该重新提示用户时)。

Once a positive value has been entered, the program assigns it to a value in another class. (We're learning the MVC OOP design pattern).

输入正值后,程序会将其分配给另一个类中的值。 (我们正在学习MVC OOP设计模式)。

Double.isNaN(Double.parseDouble(h)) ---> can anyone help me find what am I missing?

Double.isNaN(Double.parseDouble(h))--->任何人都可以帮我找到我错过的东西吗?

// prompt to get bandwidth from user
// check for validity
// if invalid, prompt again
try{
    h = JOptionPane.showInputDialog("Enter bandwidth as a positive number");
        // loop until parsed string is a valid double
    while (Double.isNaN(Double.parseDouble(h)) || Double.parseDouble(h) <=0)  {
            h = JOptionPane.showInputDialog("Enter bandwidth as a positive number");
        }
        // h has been set to valid double, set it to bandwidth
        model.setBandwidth(Double.parseDouble(h));  
    }catch(NumberFormatException|NullPointerException NFE){
        System.err.println("Caught exception: " + NFE.getMessage());
    }

2 个解决方案

#1


1  

This is because of how parseDouble() works.

这是因为parseDouble()的工作原理。

Throws:

NumberFormatException - if the string does not contain a parsable double.

NumberFormatException - 如果字符串不包含可解析的double。

(See here)

So if the String is not a double parseDouble() will not return NaN but throw an exception, which means your catch clause will be called.

因此,如果String不是双重parseDouble()将不返回NaN但抛出异常,这意味着将调用您的catch子句。

To solve this problem maybe use recursively algorithm which will call your method again if an exception is thrown.

要解决这个问题,可以使用递归算法,如果抛出异常,它将再次调用您的方法。

#2


1  

As 4castle already stated, you need to move your try/catch block inside your while loop.

正如4castle所说,你需要在while循环中移动try / catch块。

However, for validating user input you can basically stick to the following pattern:

但是,为了验证用户输入,您基本上可以坚持以下模式:

public Foo getUserInput() {
    Foo result;
    do {
        try {
            String s = requestUserInput(); // something like Scanner.nextLine()
            result = parseUserInput(s); // something like Double.parseDouble(String)
        }
        catch(Exception exc) {
            // maybe you want to tell the user what's happened here, too
            continue;
        }
    }
    while(!isValid(result)); // something like (0 < result)
    return result;
}

#1


1  

This is because of how parseDouble() works.

这是因为parseDouble()的工作原理。

Throws:

NumberFormatException - if the string does not contain a parsable double.

NumberFormatException - 如果字符串不包含可解析的double。

(See here)

So if the String is not a double parseDouble() will not return NaN but throw an exception, which means your catch clause will be called.

因此,如果String不是双重parseDouble()将不返回NaN但抛出异常,这意味着将调用您的catch子句。

To solve this problem maybe use recursively algorithm which will call your method again if an exception is thrown.

要解决这个问题,可以使用递归算法,如果抛出异常,它将再次调用您的方法。

#2


1  

As 4castle already stated, you need to move your try/catch block inside your while loop.

正如4castle所说,你需要在while循环中移动try / catch块。

However, for validating user input you can basically stick to the following pattern:

但是,为了验证用户输入,您基本上可以坚持以下模式:

public Foo getUserInput() {
    Foo result;
    do {
        try {
            String s = requestUserInput(); // something like Scanner.nextLine()
            result = parseUserInput(s); // something like Double.parseDouble(String)
        }
        catch(Exception exc) {
            // maybe you want to tell the user what's happened here, too
            continue;
        }
    }
    while(!isValid(result)); // something like (0 < result)
    return result;
}