Reputation: 13
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: ""
Upvotes: 1
Views: 1133
Reputation: 101
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();
}
}
Upvotes: 0
Reputation: 25267
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.
Upvotes: 0
Reputation: 48258
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
just by doing
if (myString.isEmpty()) {
}
Upvotes: 1
Reputation: 26926
Some of the texts are empty. Is not possible to say which one. Check all the values:
Upvotes: 1