0

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;
        }
    }
}
10
  • 1
    We know that your loop body will run exactly once. The compiler doesn't know that it will run at all. Given that the loop is pointless (you're returning either way), why do you have a loop at all? Commented May 13, 2014 at 9:24
  • 1
    why loop? in first iteration it returns ... Commented May 13, 2014 at 9:24
  • 1
    Maybe he wants the return -1 at the end of the method and not inside the for? Commented May 13, 2014 at 9:26
  • your method MUST ALWAYS return an int. The compiler thinks that there is a probablity that the method might not return an int. Commented May 13, 2014 at 9:27
  • 1
    @Darshana: So this code isn't representative of your actual problem. I suggest you post representative code instead, otherwise we can't help you. In particular, you should consider what you want to happen if you don't find the file. Commented May 13, 2014 at 9:30

2 Answers 2

3

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.)

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

As stated by OP, "this is dummy code. actually my code is to find a file recursively."
I don't see how this could fix the problem, i mean: you have 192 rep you can post comments..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.