Java JOptionPane的输入验证。如何确保输入是双精度的?

Java JOptionPane的输入验证。如何确保输入是双精度的?,java,Java,你怎么能检查输入是双精度的 Double.parseDouble如果输入不是Double,将抛出NumberFormatException。如果要对异常进行处理,请捕获该异常 可以这样做: public void parse() { try { double d = Double.parseDouble(JOptionPane.showInputDialog("What is the radius of the circle?")); } catch (Numbe

你怎么能检查输入是双精度的


Double.parseDouble
如果输入不是
Double
,将抛出
NumberFormatException
。如果要对异常进行处理,请捕获该异常

可以这样做:

public void parse() {
    try {
        double d = Double.parseDouble(JOptionPane.showInputDialog("What is the radius of the circle?"));
    } catch (NumberFormatException nfe) {
        //do something with the input here
        nfe.printStacktrace();
    }
}

public void parse() {
    try {
        double d = Double.parseDouble(JOptionPane.showInputDialog("What is the radius of the circle?"));
    } catch (NumberFormatException nfe) {
        //do something with the input here
        nfe.printStacktrace();
    }
}