Java异常名称

时间:2015-11-11 13:48:08

标签: java swing exception-handling jframe

以下是我目前的代码.....它是一个Jframe,用于根据他们的BMI计算用户的理想体重。我目前有一个try,catch来测试NumberFormatException,当用户输入无效数据时,我还想测试另一件事。我想要的是:当用户在输入区域中输入NO数据时,它会引发另一个错误。我不知道这种例外的名称,即使存在例外。

 private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
    // string declaration
    String measurementSystem;
    String name;
    double height;
    double outputweight;

    // clears the output box for the next entry
    outputField.setText("");

    try {

        // variable definition, also creates
        // decimal format for rounding
        DecimalFormat df = new DecimalFormat ("#.##");
        name = inputnameField.getText();
        height = Double.parseDouble(inputheightField.getText());
        measurementSystem = inputmeasureField.getText();

        // checks for a value in measurementSystem,
        // if equal to M or I (ignoring case),
        // it will run either code.
        // if neither, it tells the user to input a valid value,
        // and resets the measure input box for them.
        if ("M".equalsIgnoreCase(measurementSystem)) {
            outputweight = height * height * 25;
            outputField.setText(name + "'s ideal weight is " + df.format(outputweight) + " kgs.");
            inputheightField.setText("");
            inputmeasureField.setText("");
            inputnameField.setText("");
        }
        else if ("I".equalsIgnoreCase(measurementSystem)) {
            outputweight = height * height * 25 / 703;
            outputField.setText(name + "'s ideal weight is " + df.format(outputweight) + " lbs.");
            inputheightField.setText("");
            inputmeasureField.setText("");
            inputnameField.setText("");
        }
        else {
            outputField.setText("Please input a valid measure system.");
            inputmeasureField.setText("");
        } 
    }

    catch (NumberFormatException e) {
        outputField.setText("Please input valid numbers.");
        inputheightField.setText("");
    }
}                                               

0 个答案:

没有答案