-->

计划跳过everyother线,但不知道为什么[复制](Program skips everyoth

2019-09-28 21:04发布

这个问题已经在这里有一个答案:

  • 扫描仪跳过从文件[复制]每秒线 5个回答

我的计划打印就行了所有数据,但只输出每隔一行。 我知道它有什么做的“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;
        }

    }

}

Answer 1:

考虑这种办法(见这里DOC ):

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


文章来源: Program skips everyother line, but not sure why [duplicate]