Program skips everyother line, but not sure why [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-11 06:46:46

问题


My program prints all the data on the line, but only prints every other line. I know that it has something to do with the "nextLine", but I cannot find whats causing the problem.

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:


Consider this approach (see doc here):

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


来源:https://stackoverflow.com/questions/12612674/program-skips-everyother-line-but-not-sure-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!