Parsing a string array in java
arrays, double, java, parsing, string
Solution
The problem you have is that you create a new array in loop. You should take it out and initialized.
String[] num = sNums.split(" ");
double[] numbers = new double[num.length]; // The valid place for loop
for (int i = 0; i < num.length; ++i)
{
numbers[i] = Double.valueOf(num[i]);
}
for(double item: numbers) {
out.print(item + " ");
}
Problem
``` sNums = scanString.nextLine(); String[] num = sNums.split(" "); for (int i = 0; i < num.length; ++i) { numbers = new double[i+1]; numbers[i] = Double.valueOf(num[i]); } for(double item: numbers) out.print(item + " "); ``` I'm trying to change the String of numbers I have which is "num" in this case into an array of double. I'm pretty sure this should work but for some reason it's storing "0.0" into every element entered except for the last one. For example if I enter "1 5 7 90 52[enter]" the output should be "1.0 5.0 7.0...etc" but instead what I get is "0.0 0.0 0.0 0.0 52.0"