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();
}
}
}
以下是錯誤訊息。
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)
uj5u.com熱心網友回復:
您必須決議字串以將它們轉換為適當的型別,例如將 a 轉換String為 a Double,您可以使用Double.valueOf()
例如:
flight.setDistanceTravelled(Double.valueOf(data[4]));
其他轉換將更具挑戰性,需要您知道檔案中日期的格式。
類通常有一個valueOf()可以使用的工廠方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/431329.html
上一篇:EclipseIDE錯誤:無法找到或加載主類javafx.fxml原因:java.lang.ClassNotFoundException:javafx.fxml
