Question

I want to write a code like this

public int recursiveMethod() {
    for (int i = 0; i < 10; i++) {
        if (someBool) {
            return recursiveMethod();
        } else {
            return -1;
        }
    }
}

but this gives compile error missing return statement. Is there other way I can achieve this.

UPDATE : actual code

public static File searchFile(File currentFile) {
    File[] results = null;

    if (currentFile.isDirectory()) {
        for (File file : currentFile.listFiles()) {
            if (file.isDirectory()) {
                return searchFile(file);
            } else {
                results = file.getParentFile().listFiles(new FilenameFilter() {
                    public boolean accept(File dir, String name) {
                        return name.toLowerCase().endsWith(".sh");
                    }
                });

                if (results.length > 0) {
                    return results[0];
                } else {
                    return null;
                }
            }
        }
    } else {
        results = currentFile.getParentFile().listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".sh");
            }
        });

        if (results.length > 0) {
            return results[0];
        } else {
            return null;
        }
    }
}
Was it helpful?

Solution

Your code is broken in the way it's looping - you're stopping on the very first iteration, either reporting success or failure. You should be continuing to loop until you find something or run out of items to iterate over.

I would change the general structure so that the last statement in the method is return null; - so any time you can return a positive result, you do so, but otherwise you just let it fall through. So something like this:

public static File searchFile(File currentFile) {
    if (!currentFile.isDirectory()) {
        throw new InvalidArgumentException("Starting point must be a directory");
    }
    for (File file : currentFile.listFiles()) {
        if (file.isDirectory()) {
            File result = searchFile(file);
            if (result != null) {
                return result;
            }
        } else if (file.getName().toLowerCase().endsWith(".sh")) {
            return file;
        }
    }
    // Not found anything: return null to indicate failure (in this branch)
    return null;
}

(I've removed the call to getParentFile() and restructured the code for simplicity. Now it will only accept a directory as the starting point, but that simplifies things greatly, and makes much more sense anyway, IMO.)

OTHER TIPS

what is someBool? You are calling recursiveMethod() if someBool is true and everytime recursiveMethod() is called, the value of i starts from 0 and goes on callingrecursiveMethod().

As mentionned in previous comment, simply putting a return statement at the end of the method does not guarantee you are achieving what you need

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top