Reputation: 685
I'm new to SO and a fairly new to Java so please don't hate on me. I have multiple editText boxes that are supposed to function like an excel spreadsheet. Here is some code from this simple application:
EditText tradeDif = (EditText) findViewById(R.id.tradeDif);
TextWatcher tradeWatch = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
calcTrade();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
};
private void calcTrade() throws NumberFormatException {
Editable eValue1 = newPrice.getText(), eValue2 = tradeIn.getText(), eValue3 = acc.getText();
Double value1 = 0.0, value2 = 0.0, value3 = 0.0, result = 0.0;
if (eValue1 != null)
value1 = Double.parseDouble(eValue1.toString());
if (eValue2 != null)
value2 = Double.parseDouble(eValue2.toString());
if (eValue3 != null)
value3 = Double.parseDouble(eValue3.toString());
if (value1 != null && value2 != null && value3 != null)
result = value1 - (value2 + value3);
tradeDif.setText(result.toString());
}
The EditText field TradeDif needs to be able to display the variable result. When I run this it crashes, and I THINK it's because the tradeDif editText is empty. Also, I need to be able to use what is displayed in the tradeDif EditText for further calculations with further editTexts. I'm assuming I would need to use some kind of try catch block, but I'm kind of unsure how to implement it. Any help is appreciated.
Upvotes: 0
Views: 83
Reputation: 17567
Double.parseDouble()
throws a NumberFormatException if the provided String
does not contain a valid double value. This includes empty String.
To pass that error, I suggest to check, if the string is empty by creating a new method:
private double toDouble(final Editable editable) {
final String content = editable.toString();
if (content.isEmpty()) {
return 0;
}
return Double.parseDouble(content);
}
And then change your code to:
if (eValue1 != null)
value1 = toDouble(eValue1);
if (eValue2 != null)
value2 = toDouble(eValue2);
if (eValue3 != null)
value3 = toDouble(eValue3);
The new method avoids code doubling and it could easily be maintained.
You could also think about catching the Exception, but I prefer avoiding them in the first place. Especially for this kind of reason (empty String).
Upvotes: 1
Reputation: 1431
Surround your if statements with a try/catch, and handle failure appropriately:
try {
if (eValue1 != null)
value1 = Double.parseDouble(eValue1.toString());
if (eValue2 != null)
value2 = Double.parseDouble(eValue2.toString());
if (eValue3 != null)
value3 = Double.parseDouble(eValue3.toString());
if (value1 != null && value2 != null && value3 != null)
result = value1 - (value2 + value3);
tradeDif.setText(result.toString());
} catch (NumberFormatException e) {
// Not a number in one of the fields, alert user
}
Upvotes: 1