简体   繁体   中英

Function not returning proper value, looking for a value in a list

I have a class that has a function like this:

public void createFlight(String aname, String orig, String dest, String id) {
    Flight f = new Flight(aname, orig, dest, id);
    boolean found = false;
    boolean quit = false;
    for (int i = 0; i < flightList.size(); i++) {
        //Check origin exist
        if (airportList.get(i).getName().equals(orig))
            found = true;
        if (!found) {
            System.out.println("Origin " + orig + " does not exist.");
            quit = true;
        }
        found = false;
        //check destination exists
        if (airportList.get(i).getName().equals(dest))
            found = true;
        if (!found) {
            System.out.println("Destination " + dest + " does not exist.");
            quit = true;
        }
        //check if origin and destination are the same
        if (orig.equals(dest)) {
            System.out.println(id + "'s Origin and destination cannot be the same.");
            quit = true;
        }
    }
    if (!quit) {
        flightList.add(f);
        System.out.println("Added flight " + id);
    }
}

If I run:

    s.createFlight("USAIR", "ACE", "YVR", "778");//origin airport does not exist
    s.createFlight("BOS", "YHZ", "YUL", "123");

Line 2 should work correctly, and line 1's origin airport does not exist. However I get this:

Added flight 778
Destination YUL does not exist.

Which is the incorrect output. My expected output would say the first flight's origin does not exist.

不知道是什么,但是循环变量“ i”应该从0运行到我认为的airPortList.size()。

You need to reset found to false at the start of the loop just before checking if the origin exists; otherwise it might have a value of true left over from the previous iteration's destination check.

for (int i = 0; i < flightList.size(); i++) {
    found = false; // <-- add this
    //Check origin exist
    if (airportList.get(i).getName().equals(orig))
        found = true;
    ...

With the way you have it now, when iterating through the flight list, if one flight's destination exists, it behaves as if the next flight's origin exists, regardless of whether it actually does or not.

This logic is a bit odd overall, however, and some simplification (such as Mohammad's answer ) could make it a little clearer and make it easier to diagnose problems like this. Also, like R Kaja Mohideen 's answer mentioned, you may not be searching the whole airport list.

Does this simplification help at all? Can you figure out what u're doing wrong?

public void createFlight(String aname, String orig, String dest, String id) {
    Flight f = new Flight(aname, orig, dest, id);
    boolean found = false;
    boolean quit = false;

    for (int i = 0; i < flightList.size(); i++) {
        //Check origin exist
        if (!airportList.get(i).getName().equals(orig))
        {
            System.out.println("Origin " + orig + " does not exist.");
            quit = true;
        }        
        //check destination exists
        if (!airportList.get(i).getName().equals(dest))
        {
            System.out.println("Destination " + dest + " does not exist.");
            quit = true;
        }
        //check if origin and destination are the same
        if (orig.equals(dest)) {
            System.out.println(id + "'s Origin and destination cannot be the same.");
            quit = true;
        }
    }
    if (!quit) {
        flightList.add(f);
        System.out.println("Added flight " + id);
    }
}

Here's what you can do:

public void createFlight(String aname, String orig, String dest, String id) {
    Flight f = new Flight(aname, orig, dest, id);
    boolean found = false;
    boolean quit = false;


    for (int i = 0; i < flightList.size(); i++) {
        //Check origin exist
        if (!airportList.get(i).getName().equals(orig))
        {
            System.out.println("Origin " + orig + " does not exist.");
            quit = true;
            break;
        }        
        //check destination exists
        else if (!airportList.get(i).getName().equals(dest))
        {
            System.out.println("Destination " + dest + " does not exist.");
            quit = true;
            break;
        }
        //check if origin and destination are the same
        else if (orig.equals(dest)) {
            System.out.println(id + "'s Origin and destination cannot be the same.");
            quit = true;
            break;
        }
    }
    if (!quit) {
        flightList.add(f);
        System.out.println("Added flight " + id);
    }
}

The for loop is iterating through all of the Flight s in the flightList instead of all of the Airport s in the Airport list.

If there are three flights in the flightList and 100 Airport s in the airportList the logic in the loop will only compare the provided flight against the first 3 airports instead of all 100. You should use airportList to determine the number of iterations, or better yet put the Airport s within a Map<String,Airport> where the key is the airport code. This will eliminate many of the flags in the code and make the conditionals more concise.

Map<String,Airport> airportMap = new HashMap<String,Airport>();
/* Populate map like airportMap.put("BOS", new Airport("BOS",..));*/

public void createFlight(String aname, String orig, String dest, String id) {

    if(airportMap.containsKey(orig) && airportMap.containsKey(dest) && !orig.equals(dest)){
         Flight f = new Flight(aname, orig, dest, id);
         flightList.add(f);
         System.out.println("Added flight " + id);
    }else if(orig.equals(dest)){
         System.out.println(id + "'s Origin and destination cannot be the same.");
    }else if(!airportMap.containsKey(orig)){
         System.out.println("Origin " + orig + " does not exist.");
    }else if(!airportMap.containsKey(dest)){
         System.out.println("Destination " + dest + " does not exist.");
    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM