程序会跳过其他所有行,但不确定原因

时间:2012-09-27 01:10:52

标签: java arrays lines skip

我的程序打印该行的所有数据,但只打印每隔一行。我知道它与“nextLine”有关,但我找不到导致问题的原因。

import java.io.*;
import java.util.*;

public class hw2
{
    public static void main (String[] args) throws Exception
    {

    String carrier;
    int flights;
    int lateflights;
    int ratio;

    String[][] flightData= new String [221][3];
    String[] temp;

    File file = new File ("delayed.csv");
    Scanner csvScan = new Scanner(file);

    int c = 0;
        while ((csvScan.nextLine()) != null){

        String s = csvScan.nextLine();

        temp = s.split(",");

        for(int i =0; i < temp.length ; i++)
            System.out.println(temp[i]);

        flightData[c][0] = temp[1];
        flightData[c][1] = temp[6];
        flightData[c][2] = temp[7];
        c = c+1;
        }

    }

}

1 个答案:

答案 0 :(得分:3)

考虑这种方法(参见doc here):

Scanner csvScan = new Scanner(file);
while (csvScan.hasNextLine()) {
    String s = csvScan.nextLine();
    // for testing
    System.out.println(s);
    // ... rest of code
}