Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DaveTaylor

macrumors 6502
Original poster
Nov 6, 2007
381
0
Aberdeenshire, Scotland
Code:
    public double calcBMI()
    {
        double BMI;
        BMI = weight / (height * height);
        return BMI;
        
       if (BMI >=30) {
            return BMI.toString + "Obese";
        }
        else if (BMI >=25) {
            return BMI.toString + "Overweight";
        }
        else if (BMI >=18.5) {
            return BMI.toString + "Normal Weight";
        }
        else {
            return BMI.toString + "Underweight";
        }
    
    }

Help? lol
 
First of all you should try
return ("<string>" + BMI);
That is, the '+' operator will perform concatenation.

Second, I don't understand when those if statements will ever execute because you have a return statement above that will always execute
 
First of all you return before you hit your if.. else statements meaning they will never get called.

Secondly your method returns a double yet you are trying to return a string in your if.. else statements.

Thirdly when calling the toString() method you need to use the object Double rather than the primitive type. So you should declare BMI as a Double (java.lang.Double) rather than double.

Here is a simple example:

Code:
public class Main
{
	public static void main(String[] args)
	{
		Double test;
		test = 0.843;
		String test_string = test.toString();
		System.out.println(test_string);
		System.out.println(test);
	}
}
 
The actual error is double cannot be dereferenced.

First, you can't do this:
Code:
... BMI.toString ...

because BMI is declared as double, the primitive type. Java does not perform autoboxing in this situation, as I recall.

If BMI were declared as Double, the wrapper class for the primitive type, then it might work...

Except that you can't do this:
Code:
... someObject.someMethod ...

because Java does not automatically add ()'s for you. If you want to invoke the toString method on an object, you have to write it as a method invocation:
Code:
... anyObject.toString() ...

If you're not using a tutorial or a book, you really should be.
 
I just wanted add to the good observations made so far by suggesting the use of java.lang.String's valueOf(double) method. You can run this and get a String back. Using the String + double operator works fine, too, but is not needed if all you want is the String representation of a double.

-Lee
 
My java is a little rusty but this should be right...

Code:
public String calcBMI(int height, int weight) {
			double BMI;
			BMI = (weight / (height * height));
			String str = “”;
			
			if (BMI >=30) {
				str = (Double.toString(BMI) + “Obese”);
				return str; 
			}
			else if (BMI >=25) {
				str = (Double.toString(BMI) + “Overweight”);
				return str; 
			}
			else if (BMI >=18.5) {
				str = (Double.toString(BMI) + “Normal Weight”);
				return str; 
			}
			else {
				str = (Double.toString(BMI) + “Underweight”);
				return str; 
			}
			
		}
 
My java is a little rusty but this should be right...

Code:
public String calcBMI(int height, int weight) {
			double BMI;
			BMI = (weight / (height * height));

When the calculation is (weight/(mass * mass)) with no other coefficients, the height units are meters and mass (weight) units are kilograms. If height can only take on integer meters (1, 2, 3), you have a serious precision problem.

http://en.wikipedia.org/wiki/Body_mass_index

Also, these types are never going to produce any fractional value in the result, because the calculation is carried out entirely in integer arithmetic (i.e. truncating division), then the integer result is converted to double. If you want a double calculation with the possibility of a fractional part, at least one operand must be of type double.

I don't think this problem is unique to Java. You'd have similar problems in C, which has similar rules for integer and double arithmetic.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.