I am trying to understand why I cannot print my desired outcome. I have changed my loop logic around a few times but cannot see the issue.
I have an array list ArrayList<Landing> landings = new ArrayList<>();
I have the following method in the Landing class to check the landing ID;
public boolean checkId(int id) {
if (this.landingId == id) {
return true;
} else {
return false;
}
}
I am trying to create some input handling where if the user inputs an ID which doesn't match an ID in the array list, an error message is printed. I can't seem to get the failed scenario to print the message
System.out.print("Please enter the landing ID: ");
int id = Integer.parseInt(sc.nextLine());
boolean unsuccessful = false;
for (int i = 0; i < landings.size(); i++) {
if (landings.get(i).checkId(id)) {
landings.get(i).printLandings();
unsuccessful = false;
break;
} else {
unsuccessful = true;
}
}
if (unsuccessful) {
System.out.println(
"\nThe ID you enterred does not match any landing");
}
break;
TIA
breakin theifbranch will just end at the first successful condition. You should break when you setunsuccessfulto true, instead. Otherwise the next successful condition will just reset the boolean, break the loop and skip the message.checkIdcan be simplified toreturn this.landingId == id;