DirkJan
DirkJan

Reputation: 581

Android: The values are not converted to their equivalent in kilograms and meters

I'm fairly new to Android App development and just learning to make some simple apps. So I decided to make a BMI calculator. I made an interface where the users will choose the units for their height and weight using a spinner. Then, upon clicking the button, I will parse the strings of the value on the EditText into double. The spinner item chosen by the user (which are the units for the height and weight) are also obtained. I managed to get the right values by using the setText and returned the values and units inputted by the user. But, the values are not converted the way I want them. The if-else statement may be at fault here.

    package com.dirk.myfirstapp;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.TextView;

    public class MainActivity extends Activity {

        TextView output;
        EditText weightInput, heightInput;
        Button calculate;
        double height, weight, BMI;
        String heightInputString, weightInputString, weightUnit, heightUnit;
        Spinner heightUnitSpinner, weightUnitSpinner;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Button calculate = (Button) findViewById(R.id.button1);
            output = (TextView) findViewById(R.id.textView4);
            weightInput = (EditText) findViewById(R.id.editText1);
            heightInput = (EditText) findViewById(R.id.editText2);
            heightUnitSpinner = (Spinner) findViewById(R.id.spinnerHeight);
            weightUnitSpinner = (Spinner) findViewById(R.id.spinnerWeight);

            calculate.setOnClickListener(new View.OnClickListener() {

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

                    //PARSE STRINGS
                    heightInputString = heightInput.getText().toString();
                    height = Double.parseDouble(heightInputString);
                    weightInputString = weightInput.getText().toString();
                    weight = Double.parseDouble(weightInputString);
                    weightUnit = weightUnitSpinner.getSelectedItem().toString();
                    heightUnit = heightUnitSpinner.getSelectedItem().toString();

                    // CONVERT FIRST TO SI UNITS

                    // CONVERT WEIGHT TO kilogrammes
                    if (weightUnit == "lbs") {
                        weight = weight * 0.453592;
                    } 
                    // CONVERT HEIGHT TO meters
                    if (heightUnit == "cm") {
                height = height / 100;
                    } else if (heightUnit == "in") {
                        height = height * 0.0254;
                    }

                    BMI = weight / (height * height);
                    output.setText("Your BMI is " + BMI);


                }
            });

        }



    }

Upvotes: 1

Views: 1715

Answers (1)

wtsang02
wtsang02

Reputation: 18863

When you compare Strings use Object.equals("string"); to compare. Don't use '==''

Ex:

// CONVERT HEIGHT TO meters
                    if (heightUnit.equals("cm")) {
                height = height / 100;
                    } else if (heightUnit.equals("in")) {
                        height = height * 0.0254;
                    }

Upvotes: 1

Related Questions