java.lang.NumberFormatException: empty String

前端 未结 2 661
醉梦人生
醉梦人生 2020-11-27 23:45

The code below keeps giving a java.lang.NumberFormatException: empty String:

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt         


        
相关标签:
2条回答
  • 2020-11-28 00:06

    The string you're trying to parse as double is empty. You need to check if the getText() method returns a non empty string before trying to do the parsing cause you can't parse to double an empty string.

    0 讨论(0)
  • 2020-11-28 00:08

    You should check your field before parse double:

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
       double AText = ParseDouble(angleAField.getText());
       double BText = ParseDouble(angleBField.getText());
       double CText = ParseDouble(angleCField.getText());
       double aText = ParseDouble(sideaField.getText());
       double bText = ParseDouble(sidebField.getText());
       double cText = ParseDouble(sidecField.getText());
    
    // other code here same
    }
    
    double ParseDouble(String strNumber) {
       if (strNumber != null && strNumber.length() > 0) {
           try {
              return Double.parseDouble(strNumber);
           } catch(Exception e) {
              return -1;   // or some value to mark this field is wrong. or make a function validates field first ...
           }
       }
       else return 0;
    }
    
    0 讨论(0)
提交回复
热议问题