Hi guys,
I seem to have an issue with a boolean method returining a value. Here is the code excerpt - irrelevant code omitted:

public class StreamsTest {

    public static void main(String[] args) {
        String[] arr = new String[]{"a", "b", "c"};         
        //convert array to string with streams
        List<String> list = Arrays.stream(arr).collect(Collectors.toList());        
        isElementInIt(list);   
    }

    static boolean isElementInIt(List<String> list) {
        if(list.isEmpty()) {
            return false;
        }
        else {
            for (String string : list) {
                if (string.contains("a")) {
                    return true;
                }
                else {
                    return false;
                }
            }
        }       
    }
}

STS says that the isElementInIt method should return a boolean value, but it's already doing that whatever happens, so I'm not too sure why the error. Any idea?
thanks

It's just the limited intelligence of the compiler. You and I can see that you always return a boolean, but the compiler sees that you may exit the for loop without returning a boolean. Just add a (dummy) return false; at the end and the cpmpiler will be happy.

Right, lol. I find it a bit odd but yes, understood :-), thanks!

Sorry, but you do realize that that only investigates the first element in the list, right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.