Exception found trying to parseDouble

痴心易碎 提交于 2019-12-11 07:28:30

问题


I am trying to make an app to calculate CGPA. I have used my numeric data to save in Double variable type.

On executing, it showed

java.lang.NumberFormatException: Invalid double: "" error on my logcat.

It showed error on this segment of code:

 total_credit = Double.parseDouble(ch1.getText().toString()) + 
        Double.parseDouble(ch2.getText().toString()) + 
        Double.parseDouble(ch3.getText().toString()) + 
        Double.parseDouble(ch4.getText().toString()) + 
        Double.parseDouble(ch5.getText().toString()) + 
        Double.parseDouble(ch6.getText().toString());

I tried to handle the exception as :

try {
    total_credit = Double.parseDouble(...);
} catch (NumberFormatException e) {
    total_credit = 0.0;
}

On executing now, error has been occured on same line and error is:

ComponentInfo{com.example.aashish.cgpacalculator/com.example.aashish.cgpacalculator.calculator_page}: java.lang.NumberFormatException: Invalid double: ""


回答1:


You got an java.lang.NumberFormatException and as the message suggest, the reason is that you are trying to parse or convert or a double something that is not(in your case an empty string)

How can be fixed?

you can always verify if the widget is holding an empty string before doing any conversion

just by doing

if (myString.isEmpty()) {

}



回答2:


Some of the texts are empty. Is not possible to say which one. Check all the values:

  • ch1.getText()
  • ch2.getText()
  • ch3.getText()
  • ch4.getText()
  • ch5.getText()
  • ch6.getText()



回答3:


You need to check whether any text is empty or not for each and every control ch1, ch2, ch3, ch4, ch5, ch6.

You can use TextUtils.isEmpty(ch1.getText()) which returns boolean.




回答4:


if(ch1.getText().toString().trim().equalsIgnoreCase("") || ch2.getText().toString().trim().equalsIgnoreCase("") || ch3.getText().toString().trim().equalsIgnoreCase("") || ch4.getText().toString().trim().equalsIgnoreCase("") || ch5.getText().toString().trim().equalsIgnoreCase("") || ch6.getText().toString().trim().equalsIgnoreCase("") ){
    // empty String
    }
    else{
    try {
            double number1 = Double.parseDouble(ch1.getText().toString().trim());
            double number2 = Double.parseDouble(ch2.getText().toString().trim());
            double number3 = Double.parseDouble(ch3.getText().toString().trim());
            double number4 = Double.parseDouble(ch4.getText().toString().trim());
            double number5 = Double.parseDouble(ch5.getText().toString().trim());
            double number6 = Double.parseDouble(ch6.getText().toString().trim());

        double total_credit = number1 + number2 + number3 + number4 + number5 + number6;

    } catch (Exception e) {
            Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
        }
}


来源:https://stackoverflow.com/questions/37817311/exception-found-trying-to-parsedouble

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!