[Java] double cannot be dereferenced

Discussion in 'Mac Programming' started by DaveTaylor, Jan 25, 2010.

  1. DaveTaylor macrumors 6502

    Joined:
    Nov 6, 2007
    Location:
    Aberdeenshire, Scotland
    #1
    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
     
  2. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #2
    Can you post the actual error? Why is your return statement before the end of your method?
     
  3. stanl8 macrumors newbie

    Joined:
    Dec 24, 2009
    Location:
    Manchester, UK
    #3
    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
     
  4. DaveTaylor thread starter macrumors 6502

    Joined:
    Nov 6, 2007
    Location:
    Aberdeenshire, Scotland
    #4
    The actual error is double cannot be dereferenced.
     
  5. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #5
    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);
    	}
    }
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    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.
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    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
     
  8. cx300 macrumors member

    Joined:
    Sep 12, 2008
    Location:
    Clermont, FL
    #8
    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; 
    			}
    			
    		}
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    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.
     

Share This Page