James Connigan
James Connigan

Reputation: 175

Java break out of for loop after result found

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.

Upvotes: 0

Views: 1683

Answers (4)

Turing85
Turing85

Reputation: 20185

Easiest thing would be to return; if a match is found and add a System.out.println("Nothing found"); after the foreach-loop.

Notice that this an unusual use case. We would normally return a match (e.g. the index, or the mediaTitle, or a boolean). If we return a reference type, we could let the method return, for example, an Optional<String>, then return Optional.of(mediaByTitle) if a match is found or an Optional.empty() if no match is found. We can then decide on the calling side, through the returned Optional, what to print.

Upvotes: 1

Shivam Puri
Shivam Puri

Reputation: 1666

You can always add a boolean flag. Set it according to what logic you need, if the item was found in the list or not. Something like this;

public void findMedia(String title) {
    boolean isFound = false;
    for(Media mediaByTitle : list) {
        if (mediaByTitle.getTitle().equals(title)) {
            System.out.print(mediaByTitle.toString());
            //change boolean value to true as item was found.
            isFound = true;
            break;
        }
    }
    //since you want to print something when item is not in list, do something like this.
    if(!isFound) {
            System.out.print("Title does not exist\n"); 
    }
}

Upvotes: 0

g00se
g00se

Reputation: 4296

Your most flexible option if you're searching a list of Media for a particular title would be a method that returns a reference to the master object already in the list. You could do something like:

public Media findMedia(String title) {
   return list.stream().filter(m -> m.getTitle().equals(title)).findFirst().orElse(null);
}

Or do the same with a for loop if you prefer

Upvotes: 0

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79808

There are at least two reasonable approaches to this. The first is simply to return from the method once a match is found, and print the "not found" message after the loop.

public void findMedia(String title) {
    for(Media mediaByTitle : list) {
        if (mediaByTitle.getTitle().equals(title)) {
            System.out.print(mediaByTitle.toString());
            return;
        }
    }

    System.out.println("Title does not exist"); 
}

A slightly cleaner way would be to have the method return the string, rather than print it, and do the printing outside of the method itself.

public String findMedia(String title) {
    for(Media mediaByTitle : list) {
        if (mediaByTitle.getTitle().equals(title)) {
            return mediaByTitle.toString();
        }
    }

    return "Title does not exist"; 
}

then, just write System.out.println(findMedia("Whatever")); to use this.

Upvotes: 0

Related Questions