Reputation: 13
I'm writing an BMI Calculator as an exercise, managed to do it all, except one issue. The app keep crashing when no number is entered and the button is pressed. I have tried setError
as you can see in my code, but it is not working. Any ideas?
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
TextView textResult;
TextView feedbackOut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText massInputText = findViewById(R.id.massText);
final EditText heightInputText = findViewById(R.id.heightText);
textResult = findViewById(R.id.textResult);
final DecimalFormat df = new DecimalFormat("0.00");
final Button buttonCalculator = findViewById(R.id.button);
feedbackOut = findViewById(R.id.feedBack);
buttonCalculator.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (massInputText.getText().length() == 0) {
massInputText.setError("Please");
}
if (heightInputText.getText().length() == 0) {
heightInputText.setError("Please");
}
double mass = Double.parseDouble(massInputText.getText().toString());
double height = Double.parseDouble(heightInputText.getText().toString());
double bmi = getMassData(mass, height);
textResult.setText(df.format(bmi).toString());
}
});
}
public double getMassData(double mass, double height) {
double square = Math.pow(height, 2);
double result = mass / square;
if (result < 18.5) {
feedbackOut.setText("You are underweight. You need to eat more! ");
} else if (result >= 18.5 & result <= 24.9) {
feedbackOut.setText("You are in the Healthy weight range. Good Job! ");
}
if (result >= 25 & result <= 50) {
feedbackOut.setText("You are Overweight. Start working out and eat more healthy! ");
}
return result;
}
}
Upvotes: 0
Views: 46
Reputation: 14621
I would suggest to improve the validation of your OnClicklistener
attached to the buttonCalculator
widget.
Especially you should you should fail fast after an invalid input, give meaningful error messages with instructions for the end user to quickly be able to understand what to do after an error occurred.
Something along these lines:
buttonCalculator.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Editable massInputStr = massInputText.getText();
if (isEmpty(massInputStr) || isNotANumber(massInputStr)) {
massInputText.setError("Please enter the mass value (example: 12.5)");
return; // Prevent further execution
}
Editable heightStr = heightInputText.getText();
if (isEmpty(heightStr) || isNotANumber(heightStr)) {
heightInputText.setError("Please enter a valid height (example: 1.75)");
return; // Prevent further execution
}
double mass = Double.parseDouble(massInputStr.toString());
double height = Double.parseDouble(heightStr.toString());
double bmi = getMassData(mass, height);
textResult.setText(df.format(bmi));
}
private boolean isEmpty(Editable editable) {
return editable == null || editable.toString().trim().isEmpty();
}
private boolean isNotANumber(Editable editable) {
return !editable.toString().trim().matches("\\d+(\\.\\d+)?");
}
});
In this specific case you should check if the input is either empty (or if it has only spaces) and if it has a specific format. This you could do with a regular expression (see isNotANumber
).
Upvotes: 1