Skip to content
Advertisement

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

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.

JavaScript

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

Advertisement

Answer

You can do this in your calculate BMI

JavaScript

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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement