简体   繁体   中英

Parsing a string array in java

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"

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM