I have this class
public void findMedia(String title) {
for(Media mediaByTitle : list) {
if (mediaByTitle.getTitle().equals(title)) {
System.out.print(mediaByTitle.toString());
} else {
System.out.print("Title does not exist\n");
break;
}
}
}
What I want to do is stop the loop when the result is found.
I understand the logic it is searching every element of the list, however how I can it to print "Title does not exist" is nothing is found.
This will work fine to print the list if found, however if not found it is just a blank line.
public void findMedia(String title) {
for(Media mediaByTitle : list) {
if (mediaByTitle.getTitle().equals(title)) {
System.out.print(mediaByTitle.toString());
}
}
}
I have tried a couple different way to get the logic straight but I think I am out of braincells at the moment.
return;
if a match is found and add aSystem.out.println("Nothing found");
after theforeach
-loop.