1

我的程序打印行上的所有数据,但只打印每隔一行。我知道它与“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;
        }

    }

}

4

1 回答 1

3

考虑这种方法(请参阅此处的文档):

Scanner csvScan = new Scanner(file);
while (csvScan.hasNextLine()) {
    String s = csvScan.nextLine();
    // for testing
    System.out.println(s);
    // ... rest of code
}
于 2012-09-27T01:20:31.133 回答