JoeLee JoeLee - 1 year ago 79
Android Question

Receiving wrong result from calculation

The maximum heart rate is the highest heart rate obtained during maximum exercise. The difference in maximum heart rate depends on age and gender. There are simple methods to gauge your predicted maximum heart rate such as the formula 220 - your age = predicted maximum heart rate for a male. For instance, a 40-year-old male’s predicted maximum heart rate is 180 beats/minute.

The program will need to calculate the maximum heart rate for a male using the formula: 220-their age=maximum heart rate and: 212-their age=maximum heart rate for a female. The user must be able to input their age and select their gender and select their resting heart rate.
The program must then use this formula to determine your target heart rate:

Target Heart Rate = [(Maximum Heart Rate – Resting Heart Rate) × 80%]

The program must be able to display the use’s maximum heart Rate, resting heart rate and Target Heart Rate.

but for some reason im receiving when i run my program it does not seem to fully take in the inputs and gives me a wrong answer when the button is clicked for male it gives me the answer of 179.0 for male and 169.0 for female.

ive looked over and over it and cannot seem to figure out this logical error. here is my code.

public class MainHMR extends Activity implements View.OnClickListener {

Button chkCmd;
CheckBox Male, Femail;
EditText age, RestHeart;
TextView MaxHeart, TargetHeart;

int m = 220;
int f = 212;
int AgeResult =0, RestResultFinal =0, TargetHeartFinal =0;
double MaleResult, FemailResult;

public void onCreate(Bundle savedInstanceState) {


private void Initilise() {
// TODO Auto-generated method stub
/* Button */chkCmd = (Button) findViewById(;
/* checkBox */Male = (CheckBox) findViewById(;
/* checkBox */Femail = (CheckBox) findViewById(;
/* EditText */age = (EditText) findViewById(;
/* EditText */RestHeart = (EditText) findViewById(;
/* TextView */TargetHeart = (TextView) findViewById(;

private void format() {
// TODO Auto-generated method stub

String change = age.getText().toString();
AgeResult = 0; // set it to 0 as the default
try {
AgeResult = Integer.parseInt(change);
catch (NumberFormatException e){}

String changeTwo = RestHeart.getText().toString();
RestResultFinal = 0; // set it to 0 as the default
try {
RestResultFinal = Integer.parseInt(changeTwo);
catch (NumberFormatException e){}

public void onClick(View v) {
// TODO Auto-generated method stub

if (Male.isChecked()) {
int result = m - AgeResult;
MaleResult = ((result - RestResultFinal) * 0.8);
String finalResult = Double.toString(MaleResult);
} else if (Femail.isChecked()) {
int result = f - AgeResult;
FemailResult = (int) ((result - RestResultFinal) * 0.8);
String finalResult = Double.toString(FemailResult);


Answer Source

Well, I could understand it if you were getting 176 for a male and 169 for a female... but not 179. Are you sure about that?

I suspect Integer.parseInt is failing for you - but you can't tell, because of this:

catch (NumberFormatException e){}

Never just catch an exception and completely ignore it. Put logging in there at the very least.

You're doing the same for both inputs. Why would you want to continue (and put up bogus data) if either parse operation failed? Surely you should just stop at that point, and inform the user that they should fix their input.

Next there's the way that you're performing the arithmetic. You're doing it all in integer arithmetic, but then using Double.toString - why? That's only ever going to give you a value ending in .0. If you want to actually get a non-integer result, you need to perform the arithmetic in floating point. e.g.

// I've changed the names of some variables to comply with conventions
double maximumRate = MALE_MAXIMUM_RATE_LIMIT - parsedAge;
double targetRate = (maximumRate - parsedRestingRate) * 0.8;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download