2
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"

2
  • 5
    You are creating a new array each time in side for loop. Commented Nov 18, 2013 at 9:36
  • Well...this is embarrassing. Thanks though, I had been pondering on this for way too long Commented Nov 18, 2013 at 9:42

5 Answers 5

5

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 + " "); 
  }
Sign up to request clarification or add additional context in comments.

1 Comment

This won't work. ArrrayIndexOutOfBoundsException at out.print(numbers[i] + " ");
2

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 + " "); 
    }

7 Comments

@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.
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 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.
@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().
The Double.valueOf() is a static method in class that return primitive double, no object there. What I tried to say is that as you do not allocate the array. You do not need that for with index i. for(String num : sNUms.split(" ") { numbers.add(Double.valueOf(num)); }, is enough.
|
0

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]); 
}

Comments

0
  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 + " "); 
    

Comments

-1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.