String numbers = ("5,25,77,44,64,8,55,28,86,35");
I would like to return an array of double values from the string. I am unable to do that successfully with the following code :
public static double[] fillArray(String numbers)
{
String[] answers = numbers.split(",");
double[] boom = new double[answers.length];
for (int index = 0; index < answers.length; index++)
{
boom[index] = Double.parseDouble(answers[index]);
}
return boom;
}
The error I get is quite weird...
[De56f047c
Why is that and how do I fix it?
You're calling toString()
on an array - and arrays don't override Object.toString()
. So you get the behaviour from Object.toString()
:
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
Call Arrays.toString(double[])
on the result instead and it will be fine. So this code:
double[] array = fillArray("5,25,77,44,64,8,55,28,86,35");
System.out.println(Arrays.toString(array));
... gives a result of:
[5.0, 25.0, 77.0, 44.0, 64.0, 8.0, 55.0, 28.0, 86.0, 35.0]
I really wish toString()
had been overridden for all arrays. It would make life simpler...
See more on this question at Stackoverflow