I have a for loop I’m using to iterate through an arraylist, and once I find the object I need I want to set a previously obtained object in a set method for this object, set a flag for ‘not found’ to false and then break out of the loop. After that I want to throw an exception if the not found flag is still true, otherwise just come to the end of the method.
I think I’m either misusing the break or the for loop. I am constantly getting the exception thrown.
Note, ‘items’ is the arraylist of LibraryItems.
for(LibraryItem l : items) {
if(l.equals(item)) {
l.setCheckedOut(patronToRetrieve);
itemNotFound = false;
break;
} else {
itemNotFound = true;
}
}
if (itemNotFound = true) {
throw new CheckInOutException("The item " + item.getTitle() + " does not exist in the catalogue. Sorry, " + patronToRetrieve.getName() + ", you will not be able to check this out at this time.");
} else {
}
One issue I could see is:
Above statement always results in
true, because you are assigningtruetoitemNotFoundin if clause.should be:
(or)
==is eqaulity check,=is assignment.