0

So I need to convert and array of Strings that I'm pulling from an API to an array of Doubles so I can use them in some calculations. Here's what I was testing with:

public static double[] ConvertStringArrayToDoubleArray(String[] sArr)
{
    double[] dArr = new double[sArr.length];

    for (int i = 0; i < sArr.length; i++) 
    {
        try 
        {
            dArr[i] = Double.parseDouble(sArr[i]);
        }
        catch (NumberFormatException e) 
        {
            System.out.println("Could Not Convert Properly at position " + 
            String.valueOf(i));
        }
    }
    return dArr;
}

public static void main(String[] args) throws Exception
{
   String[] dmgs = new String[5];
   dmgs[0] = "1.2";
   dmgs[1] = "1.3";
   dmgs[2] = "";
   dmgs[3] = "-";
   dmgs[4] = "8";
   double[] dDmgs = new double[dmgs.length];
   dDmgs = ConvertStringArrayToDoubleArray(dmgs);
   for (int i = 0; i < dDmgs.length; i++) 
   {
       System.out.println(dDmgs[i]);
   }
}

When I ran it I was given this as the output:

Could Not Convert Properly at position 2 Could Not Convert Properly at position 3 1.2 1.3 0.0 0.0 8.0

Which is what I wanted. But when I run it with this array:

[1.5, 1.5, 2.7, 0.3, 1, 6, 10, 8, 3.5, 3, 8.5, 5, 2, 6, 6, 16, 17, 16, 15, 5, 16, 15, , , , 7, 3, 3, 6, 3, 4.5, 3, 5, 8, 6, 4.5, 3, 2.2, 6, 13, 9, 4, 7, 8, 5, 1.35, 2.7, 9, 8.5, 8, 9, 8.5, 8, 8, 6, 6, 7, 6, 6.5, 5, 3, -, 2, -, 0.2, 0.2, 0.2, 3, 3, -, -]

I get this instead:

[1.5, 1.5, 2.7, 0.3, 1.0, 6.0, 10.0, 8.0, 3.5, 3.0, 8.5, 5.0, 2.0, 6.0, 6.0, 16.0, 17.0, 16.0, 15.0, 5.0, 16.0, 15.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 8.0, 6.0, 4.5, 3.0, 2.2, 6.0, 13.0, 9.0, 4.0, 7.0, 8.0, 5.0, 1.35, 2.7, 9.0, 8.5, 8.0, 9.0, 8.5, 8.0, 8.0, 6.0, 6.0, 7.0, 6.0, 6.5, 5.0, 3.0, 0.0, 2.0, 0.0, 0.2, 0.2, 0.2, 3.0, 3.0, 0.0, 0.0]

*I'm using Arrays.toString() to print these so that's why there are the spaces.

Anyways, for the life of me I can't figure out what is going on the conversion's at positions 22 through 28 since it seems to be working fine and not bugging out in the test I ran. I would assume it would just have only position 22 through 24 set to 0.0 and then continue on at position 25 with 7.0 but its not doing that.

How do I fix this?

c0der
  • 18,467
  • 6
  • 33
  • 65
  • Your input is inconsistent with the output you are getting, leading me to believe that one or both may be inaccurate. In any case, your `ConvertStringArrayToDoubleArray()` method looks correct, and your test cases pass as expected. So start with that and then proceed. – Tim Biegeleisen Jun 25 '17 at 13:52
  • 1
    Post a complete program, containing the input, and reproducing the error. – JB Nizet Jun 25 '17 at 14:08
  • how are you inputting the array of strings? – Rajeev Singh Jun 25 '17 at 14:26

1 Answers1

0

After 16, 15 and before 8, 6 in your string array it would seem from your commas that there are 11 elements (the first 3 of them empty strings). In your double array after 16.0, 15.0 and before 8.0, 6.0 there are only 7 elements (all of them 0.0). So assuming these come from the same run before and after calling ConvertStringArrayToDoubleArray() the obvious explanation is that some of your strings contain commas and for that reason cannot be parsed as doubles. However, as your first output shows, your method should print messages about these. Since none of these strings can be parsed to 0.0, aren’t there 7 messages in your output about string positions that cannot be converted properly?

For example, say that your array contains these 7 strings: "", "", "", "7, 3", "3, 6", "3, 4.5", "3, 5". This would explain why the output of the string array contains …, , , , 7, 3, 3, 6, 3, 4.5, 3, 5, … while the output of your double array contains …, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ….

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161