尝试剖析Double时发现异常 [英] Exception found trying to parseDouble

查看:100
本文介绍了尝试剖析Double时发现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个用于计算CGPA的应用.我已使用数字数据保存为Double变量类型.

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

执行时显示

java.lang.NumberFormatException:无效的双精度字符:" 我的logcat错误.

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

我试图将异常处理为:

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:无效的双精度字符:"

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

推荐答案

您收到了 java.lang.NumberFormatException ,并且如消息所示,原因是您试图解析或转换或一双不是的东西(在您的情况下为空字符串)

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)

您始终可以在执行任何转换之前验证小部件是否持有空字符串

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

随便做

if (myString.isEmpty()) {
    
}

这篇关于尝试剖析Double时发现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆