-3

I'm making an BMI calculator for a diet programming I'm making for a piece of coursework. Originally I was making a a couple variables public static to get variables from another class. My BMI calculator worked fine this way.

I then figured out that I could use a get method (for more marks). So I changed by the previous variables to private and used a get method. But when I run this program I get NaN when the program prints out the the variable that holds the BMI, this never happened before!

Can anyone help?

import java.util.Scanner;

public class Weight {
private Scanner input;
private String readInput;
private String userWeightIsPounds;
private String userWeightIsStones;

private Scanner input2;
public static double userWeight;

public Weight(){
    userWeightIsPounds = ("Pounds");
    userWeightIsStones = ("Stones");        
}

public void findOutUserWeightMessage(){
    System.out.println("Firstly Do you weigh yourself in pounds or stones?");
}

public void findOutUserWeight(){
    input = new Scanner (System.in);
    readInput = input.nextLine();
    if(readInput.equals(userWeightIsPounds)){
        System.out.println("Ok then, enter your weight in pounds please.");
    }
    if(readInput.equals(userWeightIsStones)){
        System.out.println("Ok enter your weight in stones please.");
    }   

    input2 = new Scanner (System.in);
    userWeight = input2.nextFloat();
    if (userWeight > 20){
        System.out.println("You've enetered your weight as " + userWeight + " lbs. I'll save that information for later.");
    }else{
        userWeight = userWeight * 14;
        System.out.println("I've converted your weight into pounds for you. You weigh " + userWeight + " lbs. I'll save that information for later.");  
    }
}

public double static getUserWeight(){
    return userWeight;
}

}

And there is come code the the class that does the calculations. Ignore some of the println's I was trying to find out what was happening with my variables.

public class BMI {

private double userHeightSqaured;
private double bmiMutiplier;
private double weightDivideHeight;
private double userBmi;
private double userWeightBmi;
private double userHeightBmi;



BMI(){
    bmiMutiplier = 703;
    userWeightBmi = Weight.getUserWeight();
    userHeightBmi = Height.getUserHeight();
}

public void startUpBmiMessage(){
    System.out.print("Lets start with your BMI then shall we? ");
}

public void calculateUserBmi(){
    System.out.println("userWeightBmi is " + userWeightBmi);
    System.out.println("userWeightBmi is " + userHeightSqaured);

    userHeightSqaured = userHeightBmi * userHeightBmi;
    System.out.println("userHeightSqaured is " + userHeightSqaured);
    weightDivideHeight = userWeightBmi/userHeightSqaured;
    System.out.println("weightDivideHeight is " + weightDivideHeight);

    userBmi = weightDivideHeight * bmiMutiplier;
    System.out.println("weightDivideHeight is " + weightDivideHeight);
    System.out.println("bmiMutiplier is " + bmiMutiplier);


}   

public void calculateUserBmiMessage(){

    System.out.println("Your bmi is " + userBmi);

}

}

  • See http://stackoverflow.com/questions/2618059/in-java-what-does-nan-mean – Krease Dec 19 '12 at 01:00
  • Just a hint - the keyword `static` shouldn't appear anywhere in your code (except for the `main` method of course) – Bohemian Dec 19 '12 at 01:31
  • Thanks removed static. But eclipse keeps telling me that to change the modifier to static! –  Dec 19 '12 at 01:38
  • @JonathanCanning - Do you *understand* what `static` means??? What a `static` variable is? What a `static` method is? Why you can't access an instance variable from a static method? Go back to your lecture notes / text book / whatever and read up on those topics. – Stephen C Dec 19 '12 at 01:44

2 Answers2

0

It sounds like you're trying to write a Java program that performs some calculations, and the result of your calculation is NaN - you can refer to the question In Java, what does NaN mean? for some info on NaN.

As for resolving your problem without seeing any code, and assuming your calculation worked fine with the same input before, it sounds like your switch from public static variables to private ones with getters has probably left some of your variables uninitialized, so their value defaults to 0 - Division by 0 is a common cause of NaN.

Community
  • 1
  • 1
Krease
  • 15,805
  • 8
  • 54
  • 86
0

The reason for the NaN is that this statement:

    weightDivideHeight = userWeightBmi/userHeightSqaured;

divided zero by zero. In other words userWeightBmi and userHeightSqaured were both zero at that point.

The root problem seems to be that you haven't got your head around the difference between static and instance variables. And when you should / should not use the two kinds of variable.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216