gpt4 book ai didi

java - 用Java读取CSV文件并从读取的文件中提取某些数据

转载 作者:行者123 更新时间:2023-12-02 08:42:23 25 4
gpt4 key购买 nike

我已经使用 POJO 类将 csv 文件读入 Java。该文件有 19 列,带有标题,我想从其中一个标题中提取信息。我怎么做?希望得到社区的一些指导和建议。

The csv file looks like that

这是我的一些代码:

我想从原始变量访问“EWR”并打印出数据中有多少个EWR。我试图使用 getter 来提取我想要的数据,但没有成功。当我尝试打印尺寸时,我得到了 0。

public class nycflights13 {

public static void main(String[] args) {
// TODO Auto-generated method stub
NYC13data ABC = new NYC13data ();
List<Flights> EWRairport = new ArrayList <> ();
for (Flights ab : ABC.NYC13.fdata) {
if(ab.getorigin().equals("EWR")){
EWRairport.add(ab);
} else {
continue;
}
}
System.out.println(EWRairport.size());
}
}

这是我的类(class)NYC13data:

class NYC13data {
flightdata NYC13 = new flightdata ();

public flightdata getList (){
return NYC13;
}

public NYC13data () {
NYC13.readFileFromCSV("C:\\Users\\boonl\\Desktop\\R Assignment\\flights.csv");
}
}

这是我的类(class)flightdata:

class flightdata {
public List<Flights> fdata;

public List<Flights> readFileFromCSV (String fileName){
Path pathToFile = Paths.get(fileName);

try(BufferedReader br = Files.newBufferedReader(pathToFile,
StandardCharsets.US_ASCII)){
br.readLine();
String line = br.readLine();
while (line != null) {
String [] variable = line.split(",");

//convert string array to list
List<String> list = Arrays.asList(variable);
if(list.contains("NA")) {
line = br.readLine();
continue;
} else {
Flights dataset = createFlights(variable);
fdata.add(dataset);
line = br.readLine();
}

}
}catch (IOException ioe) {
ioe.printStackTrace();
}

return fdata;

}

//Did not show all the 19 variables
private static Flights createFlights (String [] metadata) {
int year = Integer.parseInt(metadata[1]);
int month = Integer.parseInt(metadata[2]);
int day = Integer.parseInt(metadata[3]);
String flight = metadata[11];
String tailnum = metadata[12];
String origin = metadata[13];
String dest = metadata[14];
String time_hour = metadata[19];

return new Flights(year, month, day, dep_time, sched_dep_time, dep_delay, arr_time,
sched_arr_time, arr_delay, carrier, flight, tailnum, origin, dest, air_time,
distance, hour, minute, time_hour);
}

public flightdata () {
fdata = new ArrayList <> ();
}

public void printdata () {
for(Flights ab : fdata) {
System.out.println(ab);
}
}
}

这是我的类(class)航类:

class Flights {

private int year; private int month; private int day; private int dep_time;
private int sched_dep_time; private int dep_delay; private int arr_time;
private int sched_arr_time; private int arr_delay; private String carrier;
private String flight; private String tailnum; private String origin;
private String dest; private int air_time; private int distance;
private int hour; private int minute; private String time_hour;

public Flights(int year, int month, int day, int dep_time, int sched_dep_time,
int dep_delay, int arr_time, int sched_arr_time,
int arr_delay, String carrier, String flight, String tailnum,
String origin, String dest, int air_time, int distance,
int hour, int minute, String time_hour) {
this.year = year; this.month = month; this.day = day; this.dep_time = dep_time;
this.sched_dep_time = sched_dep_time; this.dep_delay = dep_delay; this.arr_time = arr_time;
this.sched_arr_time = sched_arr_time; this.arr_delay = arr_delay; this.carrier = carrier;
this.flight = flight; this.tailnum = tailnum; this.origin = origin;
this.dest = dest; this.air_time = air_time; this.distance = distance;
this.hour = hour; this.minute = minute; this.time_hour = time_hour;
}

public int getyear() {return year;}
public void setYear(int year) {this.year = year;}

public int getmonth() {return month;}
public void setMonth(int month) {this.month = month; }

public int getday() {return day;}
public void setDay(int day) {this.day = day; }

public String getcarrier() {return carrier;}
public void setcarrier(String carrier) {this.carrier = carrier;}

public String getflight() {return flight;}
public void setflight(String flight) {this.flight = flight; }

public String gettailnum() {return tailnum;}
public void settailnum(String tailnum) {this.tailnum = tailnum; }

public String getorigin() {return origin;}
public void setorigin(String origin) {this.origin = origin; }

public String getdest() {return dest;}
public void setdest(String dest) {this.dest = dest; }

public String gettime_hour() {return time_hour;}
public void settime_hour(String time_hour) {this.time_hour = time_hour; }

@Override
public String toString() {
return "Flights [year=" + year +", month=" + month +", day=" + day +", dep_time=" + dep_time
+ ", sched_dep_time=" + sched_dep_time +", dep_delay=" + dep_delay +", arr_time=" +
arr_time + ", sched_arr_time=" + sched_arr_time +", arr_delay=" + arr_delay +",
carrier=" + carrier + ", flight=" + flight +", tailnum=" + tailnum +", origin=" +
origin +", dest=" + dest ", air_time=" + air_time +", distance=" + distance +",
hour=" + hour +", minute=" + minute +", time_hour=" + time_hour +"]";
}
}

如果这里有人能指导我的代码有什么问题,我将非常感激。太感谢了。

最佳答案

我想我可能已经发现了问题,您的原始 csv 文件包含单元格格式。清除除 time_hour 列之外的所有单元格的格式。

或者作为快速修复,更改循环内 nycflights13 类中的代码

if( ab.getorigin().equals( "\"EWR\"" ) ) // nycflights13 class

作为旁注和评论中提到的,下次使用 CSV 解析器,这将避免处理 CSV 文件时的许多复杂情况

关于java - 用Java读取CSV文件并从读取的文件中提取某些数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61300075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com