1

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 Answers1

3

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
}
Michael Easter
  • 23,733
  • 7
  • 76
  • 107
  • 1
    **Facepalm** Ofc. +1 This is the correct answer. – Brian Sep 27 '12 at 01:24
  • Yes, your test in the while loop actually removes a line from the Scanner. You change it to hasNextLine(), and you will stop skipping lines. – digidigo Sep 28 '12 at 04:41