2 Replies - 241 Views - Last Post: 06 March 2013 - 03:40 PM Rate Topic: -----

#1 mshaller808  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-February 13

Unable to get this If statement to work properly! Please help

Posted 06 March 2013 - 03:25 PM

Hey everyone. I am having difficulties in figuring out how to properly write this if statement. If the user inputs their height or weight over or under what i stated, then the program should terminate. But if they enter the height and weight within what is stated, program should continue. Regardless of what I enter for the height, it still terminates. I don't know what I'm doing wrong. I've been working on this for days. Please help will be much appreciated!

import java.util.*;
public class BMI{

   public static void main (String[ ] args){
	
	        double weightInPounds = 0;
		double heightInInches = 0;
		final double BMI;
		
		Scanner reader = new Scanner (System.in);
		
		System.out.print("Enter your height in inches: ");
		heightInInches = reader.nextDouble( );
		
		if(heightInInches < 48 || heightInInches > 80);
		   System.out.print("Invalid input.");
			   System.exit(-1);
		
		
		System.out.print("Enter your weight in pounds: ");
		weightInPounds = reader.nextDouble( );
		
		if(weightInPounds < 75 || weightInPounds > 300);
		   System.out.print("Invalid pound input.");
		      System.exit(-1);
		
			
		BMI = (703 * weightInPounds)/(heightInInches * heightInInches);
		System.out.println("\nYour BMI is: " + BMI);
		
		
		if(BMI < 18.5)
			{
			System.out.print("According to your BMI you are underweight.");
			}
		if(BMI >= 18.5 && BMI < 25)
			{
			System.out.print("According to your BMI you are normal weight.");
			}
		if(BMI >=25 && BMI < 30)
			{
			System.out.print("According to your BMI you are overweight.");
			}
		if(BMI >30)
			{
			System.out.println("According to your BMI you are obese.");
			}
		
		
			
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Unable to get this If statement to work properly! Please help

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8037
  • View blog
  • Posts: 13,755
  • Joined: 19-March 11

Re: Unable to get this If statement to work properly! Please help

Posted 06 March 2013 - 03:37 PM

Missing braces on the body of your if statement, and also a gratuitous semicolon
Indented properly, it's this:
 if(heightInInches < 48 || heightInInches > 80);   // evaluate the boolean expression and 
                                                   // throw away the result of the evaluation
 System.out.print("Invalid input.");               // always print "Invalid output"
 System.exit(-1);                                  // always exit


Was This Post Helpful? 0
  • +
  • -

#3 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Unable to get this If statement to work properly! Please help

Posted 06 March 2013 - 03:40 PM

Missing braces.

A standard 'if' statement syntax is:
if(boolean) //code to execute if true;

for one-liners,
if(boolean){
   //code to execute
   //if true
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1