I'm trying to print an array which is returned from a method, but I get the error [D@6521f956 in the terminal. As you can see from the code below, I am looping through my array to calculate Body Mass Index, which I then again put in a new array. It is this array which I am trying to return to my main method and print in the terminal, so far with no luck. Anything obvious I'm doing wrong?
public class Method {
double [] bmiCalculation (double [] height, int [] weight){
double [] bmiArray = new double[height.length];
for (int i = 0; i < height.length && i < weight.length; i++) {
double bmi = weight[i] / (height[i] * height[i]);
bmiArray[i] = bmi;
}
return bmiArray;
}
}
class Methodmain {
public static void main (String [] args) {
Method method = new Method();
double [] heightArray = {1.78, 1.67, 1.59, 1.80, 1.90};
int [] weightArray = {50, 60, 70, 80, 90};
System.out.println(method.bmiCalculation(heightArray, weightArray));
}
}
That's not an error - that's just what you get when you print out a double[]
because arrays in Java don't override toString()
.
Just use Arrays.toString(double[])
instead:
double[] result = method.bmiCalculation(heightArray, weightArray);
System.out.println(Arrays.toString(result));
See more on this question at Stackoverflow