Reputation: 41
I have read a csv file into Java using POJO class.The file has 19 column with header and I would like to extract information from one of header. How do I do that? Would appreciate some guidance and advises from the community.
Here are some of my codes:
I would like to access "EWR" from the Origin Variable and print out there are how many EWR from the data. I was trying to use a getter to extract the data I want but it did not work out. I got 0 when I tried to print the size.
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());
}
}
Here are my 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");
}
}
Here are my 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);
}
}
}
Here are my class Flights
:
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 +"]";
}
}
Would really appreciate if anyone here could guide me what is wrong with my code. Thanks a million.
Upvotes: 0
Views: 1134
Reputation: 1731
I think I may have found the issue, Your original csv file contains formatting in cells. Clear the formatting for all cells except for the time_hour
column.
Or as a quick fix, change your code in nycflights13
class inside the loop
if( ab.getorigin().equals( "\"EWR\"" ) ) // nycflights13 class
As a side note and as mentioned in comments, next time use a CSV parser, this will avoid many complications when dealing with CSV files
Upvotes: 1