Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
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"

share|improve this question
5  
You are creating a new array each time in side for loop. –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Nov 18 at 9:36
 
Well...this is embarrassing. Thanks though, I had been pondering on this for way too long –  Oscar F Nov 18 at 9:42

5 Answers

up vote 3 down vote accepted

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 + " "); 
  }
share|improve this answer
 
This won't work. ArrrayIndexOutOfBoundsException at out.print(numbers[i] + " "); –  m0skit0 Nov 18 at 9:42
 
Yep. That was the op code. I fixed it. –  Vash Nov 18 at 9:43

You're recreating the array each time in the for loop. Also you're using a for each at the second for and not using it, using i instead. That would not compile since i was never declared in that scope.. Anyway I suggest you forget about arrays in Java and use Lists, they're much more convenient.

    sNums = scanString.nextLine();    
    final String[] num = sNums.split(" ");    
    final List<Double> numbers = new ArrayList<Double>();
    for (final String cur: num) {    
        numbers.add(Double.valueOf(cur));
    }    
    for(final Double item: numbers) {    
        out.print(item + " "); 
    }
share|improve this answer
 
@sᴜʀᴇsʜᴀᴛᴛᴀ, It for sure not perfect. If you use array list you can replace the first loop with foreach. And there is not need to introduce List and Object type. From my point of view this is over engineered. –  Vash Nov 18 at 9:47
1  
That code will not throw ArrayIndexOutOfBoundsException It will not compile as the i was not defined. And the OP has changed the question so you answer is not valid to the question. –  Vash Nov 18 at 9:50
 
@Vash You're correct, it will not compile. About the question being changed, sorry I don't have time to keep track of that. It's OP problem. –  m0skit0 Nov 18 at 11:03
 
@Vash Using List you don't need to allocate the array size explicitly. And I didn't introduce Object anywhere, OP is already using Double.valueOf(). –  m0skit0 Nov 18 at 11:05
1  
Good to know :D –  m0skit0 Nov 18 at 11:26
show 3 more comments

You're simple creating a new array with the increasing size in the loop on every iteration. You just need to create a new array once and populate the values in it, in the loop.

numbers = new double[num.length];
for (int i = 0; i < num.length; ++i) {
    // numbers = new double[i+1];   // Not needed  
    numbers[i] = Double.valueOf(num[i]); 
}
share|improve this answer
  1. You are misnaming among num and number.

  2. you are creating a new array each time the first for loop body. rather remove it from the loop body and place it before the loop statement as you were doing.

    String[] number = sNums.split(" ");
    for (int i = 0; i < number.length; ++i)    
    {    
      //  numbers = new double[i+1];   <-- commented out 
      numbers[i] = Double.valueOf(num[i]);    
    }  
    
  3. You are printing it wrong with enhanced-for loop:

    for(double item: numbers)    
        out.print(numbers[i] + " "); 
                     // <-- i may already have the value of number.length
    

    Rather directly print with item:

    for(double item: numbers)    
        out.print(item + " "); 
    
share|improve this answer

Using 'args' from command line arguments you can use:

    List<Double> numbers = new ArrayList<Double>();
    for(String a:args){
        numbers.add(Double.valueOf(a));
    }        
    System.out.println(numbers);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.