Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: BMI Calculator, (boolean problem)

1. ## BMI Calculator, (boolean problem)

Hello, I am making a BMI calculator that is pretty much complete but I am currently stuck on an issue with a boolean, here is my code :

```import java.util.Scanner;
public class BMI
{
public static void main(String[] args)
{
int choice;
double weightKG;
double weightLB;
double heightM;
double heightIN;
double BMI;
String name;

boolean metric = false;
boolean imperial = false;

Scanner kb = new Scanner(System.in);

name = kb.next();
System.out.println("Hello " + name +"! Please select a method to calculate your BMI. \n");
System.out.println("\nPress 1 for metric\nPress 2 for imperial\nPress 3 to exit");

choice = kb.nextInt();

if (choice == 1)
{
metric = true;
imperial = false;
}
if (choice == 2)
{
imperial = true;
metric = false;
}
if (choice == 3)
{
System.out.println("Exiting Program");
System.exit(0);
}

if (metric = true)
{
weightKG = kb.nextDouble();
heightM = kb.nextDouble();
BMI = (weightKG/((heightM)*(heightM)));
System.out.println(name + " your BMI is: "+ BMI);

if (BMI < 18.5)
{
System.out.println(name + ", it seems you are a bit underweight");
}
if (BMI >= 18.5 && BMI <= 25)
{
System.out.println(name + ", you have a normal weight");
}
if (BMI >= 25 && BMI <= 30)
{
System.out.println(name + ", it seems you are overweight");
}
if (BMI >= 30)
{
System.out.println(name + ", it seems you are obese");
}

}
if (imperial = true)
{
weightLB = kb.nextDouble();
heightIN = kb.nextDouble();
BMI = ((weightLB)/((heightIN)*(heightIN)))*703;
System.out.println(name + " your BMI is: "+ BMI);

if (BMI < 18.5)
{
System.out.println(name + ", it seems you are a bit underweight");
}
if (BMI >= 18.5 && BMI <= 25)
{
System.out.println(name + ", you have a normal weight");
}
if (BMI >= 25 && BMI <= 30)
{
System.out.println(name + ", it seems you are overweight");
}
if (BMI >= 30)
{
System.out.println(name + ", it seems you are obese");
}
}
}
}```
The problem is that I declared 2 booleans in the beginning to be false, but eclipse is telling me that the value of the local variable is not being used even though I have used them in the if statements to change the values depending on the user input. But as eclipse says, they are not being used so when I enter 2 for imperial, my code is treating it as if I pressed 1 for metric. How do I fix this issue with the boolean? thanks in advance !!!!

2. ## Re: BMI Calculator, (boolean problem)

if (metric = true)
= is the assignment operator
== is the compare for equality operator

It is more common to code tests of the values of boolean variables without comparing them:
```boolean aBooleanVar = false;

...// here set aBooleanVar to control the if statements below

if(aBooleanVar) {     //if true do it
or
if(!aBooleanVar) {   // if not true(false) do it```

3. ## The Following User Says Thank You to Norm For This Useful Post:

paco (September 19th, 2014)

4. ## Re: BMI Calculator, (boolean problem)

Hey, Im not sure what you mean by that given code, aBooleanVar. Also, when I change "==" to '=" I get an error "Cannot convert from int to boolean". Shouldn't it function using the assignment operator instead of when I am comparing?
I realize the issue is in here, just need to format it differently, but not sure how to do so, and my level of knowledge is very junior
```boolean metric = false;
boolean imperial = false;

if (choice == 1)
{
metric = true;
imperial = false;
}

if (choice == 2)
{
imperial = true;
metric = false;
}```

5. ## Re: BMI Calculator, (boolean problem)

I get an error "Cannot convert from int to boolean"
When you get errors, you need to post the full text of the error and the source that caused it.

6. ## The Following User Says Thank You to Norm For This Useful Post:

paco (September 19th, 2014)

7. ## Re: BMI Calculator, (boolean problem)

I mean its not an error but just the exclamation mark comes up before I even run my code on the side

8. ## Re: BMI Calculator, (boolean problem)

Hi paco,

I tested the code and it works nice.
Have a look:

```*** Code removed
}```

Regards

9. ## The Following User Says Thank You to QFox For This Useful Post:

paco (September 19th, 2014)

11. ## The Following User Says Thank You to Norm For This Useful Post:

paco (September 19th, 2014)

Hi @Norm,

Regards

13. ## The Following User Says Thank You to QFox For This Useful Post:

paco (September 19th, 2014)

14. ## Re: BMI Calculator, (boolean problem)

Originally Posted by QFox
Hi paco,

I tested the code and it works nice.
Have a look:

```*** Code removed
}```

Regards
its OK i did not use your code anyways as some parts of it I did understand

15. ## Re: BMI Calculator, (boolean problem)

Hi paco,

I'm new at the forum so I didn't 'read properly the rules', but hope you had understand it .
So did you resolve your problem?

Regards

16. ## The Following User Says Thank You to QFox For This Useful Post:

paco (September 19th, 2014)

17. ## Re: BMI Calculator, (boolean problem)

I don't see a discussion here of how booleans are used beyond syntax. Please note that we are abusing booleans here for no reason. A boolean holds a true/false state, and nothing else.

If we have two booleans that mean the exact opposite, as we see here, we have to always make sure that when we update or read one, we have to update or read the other.

This is not just annoying. It is the sort of thing that causes bugs, and makes code hard to maintain. We want to always minimize state that depends on other state, and especially in cases like this where the state change of the _other_ boolean is essentially a no-op.

Decide what you want to test, and give the variable a sane name that reflects this. And always think about what the default state is. In this case we have a flag that indicates whether or not a class will be changing its state to do things in metric or imperial measurements.

Let's assume "metric" is the default. When declaring it, or if we are setting it from some passed-in option that is not set or resolvable, we set it to "true". If some setting or option gives us a positive indication that it should be imperial, set it to false.

Now when we use it, we simple do things like "if (isMetric) ..."

Now this flag is not only dependable, our unit tests can assert that under specific conditions, the flag is always true, and under some other specific conditions it is false.

18. ## The Following User Says Thank You to jdv For This Useful Post:

paco (September 19th, 2014)

19. ## Re: BMI Calculator, (boolean problem)

thanks for the comment jdv. I realize what you are saying and I am going to redo some of the code right now and perhaps I will use 2 methods for the metric and imperial calculation, will post soon, thanks for the comments!

--- Update ---

heres what i got folks, seems to be working pretty well! thanks for all the help!

```
import java.util.Scanner;
public class BMI
{
public static void main(String[] args)
{
boolean restart = false;
while(!restart)
{
int choice;
String name;
Scanner kb = new Scanner(System.in);

name = kb.next();
System.out.println("Hello " + name +"! Please select a method to calculate your BMI. \n");
System.out.println("\nPress 1 for metric\nPress 2 for imperial\nPress 3 to exit");

choice = kb.nextInt();

while (choice == 1)
{
useMetric(name);
restart = false;
break;
}
while (choice == 2)
{
useImperial(name);
restart = false;
break;
}
while (choice == 3)
{
System.out.println("Exiting Program");
System.exit(0);
}
}
}
public static void useImperial(String name)
{
double weightLB, heightIN, BMI;
Scanner kb = new Scanner(System.in);
weightLB = kb.nextDouble();
heightIN = kb.nextDouble();
BMI = ((weightLB)/((heightIN)*(heightIN)))*703;
System.out.println(name + " your BMI is: "+ BMI);

if (BMI < 18.5)
{
System.out.println(name + ", it seems you are a bit underweight");
}
if (BMI >= 18.5 && BMI <= 25)
{
System.out.println(name + ", you have a normal weight");
}
if (BMI >= 25 && BMI <= 30)
{
System.out.println(name + ", it seems you are overweight");
}
if (BMI >= 30)
{
System.out.println(name + ", it seems you are obese");
}
}

public static void useMetric(String name)
{
double BMI, weightKG, heightM;
Scanner kb = new Scanner(System.in);
weightKG = kb.nextDouble();
heightM = kb.nextDouble();
BMI = (weightKG/((heightM)*(heightM)));
System.out.println(name + " your BMI is: "+ BMI);

if (BMI < 18.5)
{
System.out.println(name + ", it seems you are a bit underweight");
}
if (BMI >= 18.5 && BMI <= 25)
{
System.out.println(name + ", you have a normal weight");
}
if (BMI >= 25 && BMI <= 30)
{
System.out.println(name + ", it seems you are overweight");
}
if (BMI >= 30)
{
System.out.println(name + ", it seems you are obese");
}

}
}```

20. ## Re: BMI Calculator, (boolean problem)

The code uses 3 while() statements where an if/else if is more appropriate. Use a while() statement for looping.
When you learn the switch statement you'll see it's better for where the while statements are used.

21. ## Re: BMI Calculator, (boolean problem)

Originally Posted by paco
thanks for the comment jdv. I realize what you are saying and I am going to redo some of the code right now and perhaps I will use 2 methods for the metric and imperial calculation, will post soon, thanks for the comments!
Well, the metric vs. imperial flag is used for two things that I can see: display and output conversion.

The former can be driven off of the flag using things like

`... String.format("Please enter the height in %s", useMetric ? "centimetres" : "inches") ...`

The latter can be incorporated into your conversion method(s). The simplest thing is to keep all values in one or the other, and use the flag to convert to the other only if necessary. Other than temperature, most conversions are merely scaling, right?

This allows you to abstract very specific work into methods without worrying about the details of the output.