Hi everybody this is my first post (of many, I hope) and I'll start by saying that I am an absolute beginner with any programming language, so please talk to me like the baby that I am
I'm writing this BMI calculator mainly to try to teach myself a bit of code, and so far I have got far enough to have a working calulator, which I will try to post the code for on this thread somewhere.
Right now, the program asks for weight and height, and then spits out an answer - but there isn't any kind of validation involved, which is what I want to add next - details are as follows:
I want the input for height to be restricted to between 1 and 3 meters, and I want the input for the weight to be restricted to between 30 and 160 kilograms. Ideally, values outside these ranges should produce an error dialog if possible.
Thanks for any help that anyone can offer. I know I shouldn't ask anyone to write the code for me, but I'm here to learn - and I'll learn best if I can see an example of the code in action.
One of the neat things about modern languages is that you can completely separate various parts of your code. You already know how to get the data, you know how to use the data...All you need to do is come up with the code to validate. I would personally create a new method and call that from your code... Something like this (taking a snip from your code)
So now, after you get the weight, you call this method to check if it is valid. When you get back to your main, you have to decide what you want to do exactly. Should you end your program immediately with an error? Do you still want to get the height? etc.
I would create another method to validate the height in a similar fashion.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
To build on Fred's comment, I would say you should make the entire calculator its own class. Then use a do-while as part of your validation so if the user enters an invalid value, it will give the user the opportunity to re-enter the correct values. Then all you are doing in main() is calling on your BMI class. This makes it portable so you can use it with other Java programs...
Once you figure that out, you may want to move the BMI code to the constructor and/or make it static so you don't need to create an instance of BMI. Have fun playing around with it! (forgive any syntax or typing errors... I did this reply quickly)
James E Baker
Joined: Jul 28, 2011
Oops! I posted my ideas as a console window program instead of GUI, but you get the idea. You can do the same thing using Swing objects.
You would have done far better to have your BMI class with private final fields for weight height and BMI, and set the three up in a constructorAnd you can't simply use getBMI() in the main method; you must have a BMI object to work with. That would be yourBMI.getBMI(), not BMI.getBMI(); the latter won't compile. And it's not /n after printf, but %n. And your quote marks aren't in the right places.
Joined: Aug 15, 2011
Thank you all for your ideas, I'll try my best to implement them and post back with my successes / failures. Thanks again to you all - I'm finding java a very steep and confusing learning curve, but I can see it being very satisfying once I get to a proficient level
subject: Help making my BMI calculator more complex?