Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: The values are not converted to their equivalent in kilograms and meters
    primarykey
    data
    text
    <p>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.</p> <pre><code> 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); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload