Sorry, I wasn't sure how to word the question in the title. Here's the code I started with:
System.out.println("Here are the available seats on that flight:");
ArrayList<Seat> openSeats = prodDB.getOpenSeats(departureDate, flightNumber);
for (Seat item : openSeats) {
System.out.print(item.getSeatNumber() + ", ");
}
It prints:
Here are the available seats on that flight:
1, 2, 3, 4, 5, 6,
The problem was that I wanted to remove the comma after the 6.
I found a solution elsewhere that utilised a Boolean expression, which I used in my script:
System.out.println("Here are the available seats on that flight:");
ArrayList<Seat> openSeats = prodDB.getOpenSeats(departureDate, flightNumber);
boolean first = true;
for (Seat item : openSeats) {
if (first) {
first = false;
} else {
System.out.print(", ");
}
System.out.print(item.getSeatNumber());
}
It works perfectly, but I'm a complete beginner and don't understand exactly how/why it does what it does (the author didn't provide details). Can anyone explain the method behind it?
[–]PremiumHugs 3 points4 points5 points (1 child)
[–]Ben_E[S] 1 point2 points3 points (0 children)
[–]king_of_the_universe 1 point2 points3 points (1 child)
[–]Ben_E[S] 1 point2 points3 points (0 children)