Parsing a string array in java

21,012

Solution 1

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

Solution 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 + " "); 
    }
Share:
21,012
Oscar F
Author by

Oscar F

Updated on November 19, 2020

Comments

  • Oscar F
    Oscar F over 3 years
    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"

  • m0skit0
    m0skit0 over 10 years
    This won't work. ArrrayIndexOutOfBoundsException at out.print(numbers[i] + " ");
  • Damian Leszczyński - Vash
    Damian Leszczyński - Vash over 10 years
    @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.
  • Damian Leszczyński - Vash
    Damian Leszczyński - Vash over 10 years
    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.
  • m0skit0
    m0skit0 over 10 years
    @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
    m0skit0 over 10 years
    @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().
  • Damian Leszczyński - Vash
    Damian Leszczyński - Vash over 10 years
    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.
  • m0skit0
    m0skit0 over 10 years
    Sorry, Double.valueOf returns Double, not double. Yeah, you don't need the i index, same as second for.
  • Damian Leszczyński - Vash
    Damian Leszczyński - Vash over 10 years
    You have right with that. The parseDouble return double. ;-).