Exception Found Trying To ParseDouble
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 doubl
Solution 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()) {
}
Solution 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()
Solution 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.
Solution 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();
}
}
Post a Comment for "Exception Found Trying To ParseDouble"