samedi 24 février 2018

function needs to be void with if, else if, else? hash map, java

OK, very odd. Not come across this before from what I can remember.

OK, so the compiler is telling me that the following method should be void for some reason:

 public static HashMap<String, ArrayList<FlightData>> mapper(ArrayList<String> lineBuffer) {

    HashMap<String, ArrayList<FlightData>> mapdata = new HashMap<>(); //array list for Mapdata object

    for (String flightData : lineBuffer) {
        String[] str = flightData.split(",");
        FlightData flight = new FlightData(str[0], str[1], str[2].toCharArray(), str[3].toCharArray(), new Date(Long.valueOf(str[4])), Long.valueOf(str[5]).longValue()); //creating the object
        mapdata.get(flight.getFlightID()); //getting the flight data
        if (mapdata.containsKey(flight.getFlightID())) { //checking if the data for the oject contains hash key Flightdata
            mapdata.get(flight.getFlightID()).add(flight);
        } 
        else if (mapdata.containsKey(flight.getFromID())) {
            mapdata.get(flight.getFromID()).add(flight);
            ArrayList<FlightData> noID2 = new ArrayList<>(); //creating an array for noID
            noID2.add(flight);
            mapdata.put(flight.getFlightID(), noID2);
        }
        else {
            ArrayList<FlightData> noID = new ArrayList<>(); //creating an array for noID
            noID.add(flight);
            mapdata.put(flight.getFlightID(), noID);

            //  System.out.println(mapdata);

        }

        return mapdata;

    }

Which is odd, because when I remove the additional if (if else to just else) its fine:

 public static HashMap<String, ArrayList<FlightData>> mapper(ArrayList<String> lineBuffer) {

    HashMap<String, ArrayList<FlightData>> mapdata = new HashMap<>(); //array list for Mapdata object

    for (String flightData : lineBuffer) {
        String[] str = flightData.split(",");
        FlightData flight = new FlightData(str[0], str[1], str[2].toCharArray(), str[3].toCharArray(), new Date(Long.valueOf(str[4])), Long.valueOf(str[5]).longValue()); //creating the object
        mapdata.get(flight.getFlightID()); //getting the flight data
        if (mapdata.containsKey(flight.getFlightID())) { //checking if the data for the oject contains hash key Flightdata
            mapdata.get(flight.getFlightID()).add(flight);
        } else {
            ArrayList<FlightData> noID = new ArrayList<>(); //creating an array for noID
            noID.add(flight);
            mapdata.put(flight.getFlightID(), noID);
        }
      //  System.out.println(mapdata);

    }

    return mapdata;

}

I get the following:

screenshot of compiler correction suggestion

the error is telling my missing return statement/type. Hence void suggestion. any ideas why??

Any help would be great.

Cheers, Glenn

Aucun commentaire:

Enregistrer un commentaire