Ben
Ben

Reputation: 1061

Recursion with function not returning value

I've a function and I think I understand why it's returning "hi", every time, but I don't know how to address it other than setting the value to a local member of the class.

private String findDataFolder(File folder) {
    if (folder != null) {
        if (folder.listFiles() != null) {
            for (File file : folder.listFiles()) {
                if (file.isFile()) {

                    if (file.getParent().contains(
                            DATA_DIRECTORY)) {
                        System.out.println("is this happening");
                        String result = file.getParent();
                        return result;
                    }
                } else {
                    findDataFolder(file);
                }
            }
        }
    }
    return "hi";
}

"isthishappening" IS outputting every time, however, the most bottom part of recursion isn't being returned, the final call of it is, which is 'hi' and i know that's how it's intended but was wondering if there is a way of returning the value 'back up'. i've avoided as i say by this before by just setting a private member of the class to it but it would be nice to know how to not do this anymore

edit: removed the removeExtraDirectoryInformation function as it isn't really relevent and is just noise

Upvotes: 0

Views: 81

Answers (2)

Compass
Compass

Reputation: 5937

I just realized an additional flaw in the current code. Because you're going through a branch-wise search, we need to do something else rather than direct returning up the branch.

private String findDataFolder(File folder) {
    if (folder != null) {
        if (folder.listFiles() != null) {
            for (File file : folder.listFiles()) {
                if (file.isFile()) {
                    if (file.getParent().contains(
                            DATA_DIRECTORY)) {
                        System.out.println("is this happening");
                        String result = file.getParent();
                        return result;
                    }
                } else {
                    String result = findDataFolder(file);
                    if(!result.equals("hi"))
                        return result;
                }
            }
        }
    }
    return "hi";
}

So basically, this is not linear recursion, but compound recursion (or whatever it's called.) Because you're using a for-loop, we have multiple files, but the initial solution I provided would only return the first for loop's results, which is not what we're looking for.

One option would be to track everything in an Array or a List. However, since the goal is to find the correct file folder, I will make the assumption that there is only one correct answer, ergo as soon as we find one, we will send it up the path to the root.

The part where you return "hi" is actually helpful, as we know "hi" is not a folder path. As soon as folder is found, it'll send the result up the recursion tree. If it isn't found, "hi" will be returned and the for loop above can continue to the next folder.

Upvotes: 2

gawi
gawi

Reputation: 2962

You have to check if you found right directory every time you call recursive function:

private String findDataFolder(File folder) {
    if (folder != null) {
        if (folder.listFiles() != null) {
            for (File file : folder.listFiles()) {
                if (file.isFile()) {

                    if (file.getParent().contains(
                            DATA_DIRECTORY)) {
                        System.out.println("is this happening");
                        String result = file.getParent();
                        return result;
                    }
                } else {
                    String result = findDataFolder(file);
                    if (!("hi").equals(result)) { // check if we found the right directory, if so return it
                        return result;
                    }
                }
            }
        }
    }
    return "hi";
}

Upvotes: 2

Related Questions