How do I only allow the user to enter sensible height and weight values in Edit Text? Java

Tags: , , , ,



I’m making a super simple BMI calculator app using Java in android studio but I’d like to make it so that the user is only able to enter sensible values for their height and weight. I was thinking something around 50cm-220cm for height and 3kg-150kg for weight. As it stands, they can enter any number and come out with absurdly high or low BMI values. What is a super simple way of solving this?

I really appreciate any help 🙂

I’ve included my main activity code down below, as well as a link to my android studio project so far.

package com.example.bmicalc2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.telecom.TelecomManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.LineHeightSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private EditText Weight, Height;
private TextView ResultText;
String calculation, BMIResult;

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

    Weight = findViewById(R.id.Weight);
    Height = findViewById(R.id.Height);
    ResultText = findViewById(R.id.result);
}

public void calculateBMI(View view) {
    final String Wt = Weight.getText().toString();
    String Ht = Height.getText().toString();

    if (TextUtils.isEmpty(Wt)){
        Weight.setError("Please enter your weight!");
        Weight.requestFocus();
        return;
    }
    else if (TextUtils.isEmpty(Ht)){
        Height.setError("Please enter your height!");
        Height.requestFocus();
        return;
    }
    else {

    float weightValue = Float.parseFloat(Wt);
    float heightValue = Float.parseFloat(Ht) / 100;

    float bmi = weightValue / (heightValue * heightValue);

    if (bmi < 18.5){
        BMIResult = "You are in the underweight BMI range!";
    }
    else if (bmi >= 18.5 && bmi < 24.9) {
        BMIResult = "You are in the healthy weight BMI range!";
    }
    else if (bmi >= 25 && bmi >= 29.9){
        BMIResult = "You are in the overweight BMI range!";
    }
    else if (bmi > 30){
        BMIResult = "You are in the obese BMI range!";
    }

    calculation = "Result: " + String.format("%.2f", bmi) + "n" + BMIResult;
    ResultText.setText(calculation);
        }
    }
}

Google Drive Link to my project thus far: https://drive.google.com/drive/folders/1yMwnf7aF2D_gQ47v3BonBRPh9xdFv0x9?usp=sharing

Answer

You can do this in your calculate BMI

if (TextUtils.isEmpty(Wt)){
    Weight.setError("Please enter your weight!");
    Weight.requestFocus();
    return;
}
else if (TextUtils.isEmpty(Ht)){
    Height.setError("Please enter your height!");
    Height.requestFocus();
    return;
}
else if (Integer.parseInt(Wt) < 3 || Integer.parseInt(Wt) > 150) {
    Weight.setError("Please enter your weight in range of 3 to 150!");
    Weight.requestFocus();
    return;
} else if (Integer.parseInt(Ht) < 50 || Integer.parseInt(Ht) > 220) 
    Height.setError("Please enter your height in range of 50 to 220!");
    Height.requestFocus();
    return;
} else {
    double weightValue = Double.parseFloat(Wt);
    double heightValue = Double.parseFloat(Ht) / 100;
    // Your further Implementation

Here I have only added only two more conditions if (Integer.parseInt(Wt) < 3 || Integer.parseInt(Wt) > 150) and similarly for height. What I am doing is you have already check if Wt or Ht is not empty so I have directly fetched it’s Integer value using Integer.parseInt(Ht) similarly for weight (you can do this in try catch block if you want) and then I simply checked if the Integer.parseInt(Ht) similarly for weight is in the required range or not and throw error accordingly.



Source: stackoverflow