JOptionPane的输入验证。我如何确保输入是双倍的?

时间:2015-10-17 01:31:32

标签: java

  

你怎么能检查输入是否为双

    radius=Double.parseDouble(JOptionPane.showInputDialog("What is the radius of the circle?"));

1 个答案:

答案 0 :(得分:0)

如果输入不是Double.parseDouble

NumberFormatException将抛出double。如果您想对其进行操作,请抓住exception

可以做到这一点:

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();
    }
}

Source