user636363
user636363

Reputation: 3

Exiting a for loop from an if in the for loop?

I'm getting "A flight is available matching this information" "Passenger's details recorded and booking completed"

but then I'm also getting "There are no flights flying to this destination"

for (int k = 0;k <=4; k++)
{
    if (destination.equalsIgnoreCase(flights[k].getDestination()))
    {
        k = 5;

        System.out.print("\nEnter desired day of departure: ");
        day = scan.nextLine();

        System.out.println("\f");

        if (day.equalsIgnoreCase(flights[k].getDay()))
        {

            if (flights[k].getBookedSeats() < 100)
            {

                passengers[0 + bookedSeats].setName(name);
                passengers[0 + bookedSeats].setAddress(address);
                passengers[0 + bookedSeats].setEmail(email);
                passengers[0 + bookedSeats].setOnFlight(k);
                flights[k].increaseBookedSeats();

                System.out.println("\nA flight is available matching this information");
                System.out.println("Passenger's details recorded and booking completed");

            }else{
                System.out.println("\nThere are no seats available on this flight");
            }
        }else
        {
            System.out.println("\nThere are no flights flying to this destination on this day");
        }
    }else if(!destination.equalsIgnoreCase(flights[k].getDestination()) && k==4)
    {
        System.out.println("\nThere are no flights flying to this destination");
    }

}

Upvotes: 0

Views: 95

Answers (2)

Kakarot
Kakarot

Reputation: 4252

You can add a break statement inside your if condition to ensure that the loop breaks.

Upvotes: 4

Luke Willis
Luke Willis

Reputation: 8580

The issue here occurs when you find a valid flight for your passenger before the loop has finished running through each iteration. setting k to 5 in your if statement is a step in the right direction, but does not work because you then use flights[5] throughout the rest of the block.

You can use a break statement instead of k = 5, but if you want to ensure your code will be easily maintainable in the future, you can make your intentions clear by using a while loop with a boolean specifying when you are done.

int k = 0;
bool done = false;

while (!done && k <= 4)
{
    if (destination.equalsIgnoreCase(flights[k].getDestination()))
    {
        k = 5;

        System.out.print("\nEnter desired day of departure: ");
        day = scan.nextLine();

        System.out.println("\f");

        if (day.equalsIgnoreCase(flights[k].getDay()))
        {

            if (flights[k].getBookedSeats() < 100)
            {

                passengers[0 + bookedSeats].setName(name);
                passengers[0 + bookedSeats].setAddress(address);
                passengers[0 + bookedSeats].setEmail(email);
                passengers[0 + bookedSeats].setOnFlight(k);
                flights[k].increaseBookedSeats();

                System.out.println("\nA flight is available matching this information");
                System.out.println("Passenger's details recorded and booking completed");

                // added this line:
                done = true;        
            }else{
                System.out.println("\nThere are no seats available on this flight");
            }
        }else
        {
            System.out.println("\nThere are no flights flying to this destination on this day");
        }
    }else if(!destination.equalsIgnoreCase(flights[k].getDestination()) && k==4)
    {
        System.out.println("\nThere are no flights flying to this destination");
    }

    k++;
}

Some people think that using break is fine, and in many cases I agree with them; however, when you come back to look at your code (or someone else does), it's nice to know that a for loop will absolutely execute the number of times that it specifies, but a while loop could exit early. It just makes things easier in the future.

Upvotes: 0

Related Questions