问题
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