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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: BMI Calculator, (boolean problem)

  1. #1
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default 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);
     
    		System.out.println("Welcome to the Body Mass Index Calculator!\nPlease enter your name:");
    		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)
    		{
    			System.out.println("Please enter your weight in kilograms");
    			weightKG = kb.nextDouble();
    			System.out.println("Please enter your height in meters");
    			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)
    		{
    			System.out.println("Please enter your weight in pounds");
    			weightLB = kb.nextDouble();
    			System.out.println("Please enter your height in inches");
    			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. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

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

    paco (September 19th, 2014)

  4. #3
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default 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;
    		}
    Last edited by paco; September 18th, 2014 at 06:46 PM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

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

    paco (September 19th, 2014)

  7. #5
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default 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. #6
    Junior Member QFox's Avatar
    Join Date
    Sep 2014
    Posts
    3
    My Mood
    Fine
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: BMI Calculator, (boolean problem)

    Hi paco,

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

    *** Code removed
    }

    Regards
    Last edited by Norm; September 18th, 2014 at 07:21 PM. Reason: Please don't do OPs work for him

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

    paco (September 19th, 2014)

  10. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: BMI Calculator, (boolean problem)

    If you don't understand my answer, don't ignore it, ask a question.

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

    paco (September 19th, 2014)

  12. #8
    Junior Member QFox's Avatar
    Join Date
    Sep 2014
    Posts
    3
    My Mood
    Fine
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: BMI Calculator, (boolean problem)

    Hi @Norm,

    Thank you for the advert

    Regards

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

    paco (September 19th, 2014)

  14. #9
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: BMI Calculator, (boolean problem)

    Quote Originally Posted by QFox View Post
    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. #10
    Junior Member QFox's Avatar
    Join Date
    Sep 2014
    Posts
    3
    My Mood
    Fine
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default 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. #11
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default 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. #12
    Member
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default 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);
     
    		System.out.println("Welcome to the Body Mass Index Calculator!\nPlease enter your name:");
    		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);
    		System.out.println("Please enter your weight in pounds");
    		weightLB = kb.nextDouble();
    		System.out.println("Please enter your height in inches");
    		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);
    		System.out.println("Please enter your weight in kilograms");
    		weightKG = kb.nextDouble();
    		System.out.println("Please enter your height in meters");
    		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. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #14
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: BMI Calculator, (boolean problem)

    Quote Originally Posted by paco View Post
    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.

Similar Threads

  1. Boolean variable problem
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 29th, 2013, 06:54 PM
  2. Boolean problem
    By RepersGhost in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 8th, 2013, 07:05 AM
  3. boolean problem.
    By Leonardo1143 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 13th, 2013, 05:32 PM
  4. BMI calculation
    By dantheman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 28th, 2012, 09:57 PM
  5. GUI - calculate BMI (need help asap)
    By jahead in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 18th, 2011, 04:10 AM