public class Airport {
static ArrayList<Flights> allFlights = new ArrayList<Flights>();
public static void main(String[] args) throws ParseException
{
try
{
File myObj = new File("Flights.csv");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine())
{
String[] data = myReader.nextLine().split(",");
Flights flight = new Flights();
flight.setDateOfFlight(data[0]);
flight.setDepartureTime(data[1]);
flight.setArrivalTime(data[2]);
flight.setFlightDuration(data[3]);
flight.setDistanceTravelled(data[4]);
flight.setDelay(data[5]);
flight.setDepartureAirport(data[6]);
flight.setDepartureCity(data[7]);
flight.setArrivalAirport(data[8]);
flight.setArrivalCity(data[9]);
flight.setFlightNo(data[10]);
flight.setAirline(data[11]);
allFlights.add(flight);
}
myReader.close();
}
catch (FileNotFoundException e)
{System.out.println("File cannot be found.");
e.printStackTrace();
}
}
}
This below is the error message.
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method setDateOfFlight(Date) in the type Flights is not applicable for the arguments (String)
The method setDepartureTime(Time) in the type Flights is not applicable for the arguments (String)
The method setArrivalTime(Time) in the type Flights is not applicable for the arguments (String)
The method setFlightDuration(Time) in the type Flights is not applicable for the arguments (String)
The method setDistanceTravelled(double) in the type Flights is not applicable for the arguments (String)
The method setDelay(int) in the type Flights is not applicable for the arguments (String)
at Airport.main(Airport.java:26)
CodePudding user response:
You must parse the Strings to convert them to the appropriate type, for example to convert a String
to a Double
, you can use Double.valueOf()
For example:
flight.setDistanceTravelled(Double.valueOf(data[4]));
Other conversions will be more challenging, requiring you to know how dates are formatted in the file.
Often classes has a valueOf()
factory method you can use.