简体   繁体   English

递归Java方法中的return语句而不是终止方法

[英]Return statement in a recursive Java method not terminating method

I am new to recursion in Java and working on a class for searching for a file and returning the location of the file in question. 我是Java递归的新手,正在研究一个用于搜索文件并返回相关文件位置的类。

I ran into one small issue. 我遇到了一个小问题。 When the desired file is found, then the method is supposed to return the String of the file location within the "else if" block and terminate the method. 找到所需文件后,该方法应返回“ else if”块中文件位置的字符串并终止该方法。 Instead, the default String ("File Not Found") is returned instead, only used for when the desired file is not found. 而是返回默认字符串(“找不到文件”),仅用于找不到所需文件时。

I do know that the function can detect the desired file, I made a print statement (commented out) within the 'else if' block printing out the file location and that works, but again, returning a value in the "else if" block does not terminate the method and just runs its 'default' return value. 我确实知道该函数可以检测到所需的文件,我在'else if'块中做了一个打印语句(注释了),将文件位置打印出来,并且可以,但是再次在“ else if”块中返回了一个值不会终止该方法,而只是运行其“默认”返回值。

Any ideas or suggestions? 有什么想法或建议吗?

import java.io.File;
import java.util.*;

public class FileSearch {

    public static String findFile(File path, String target) {

        if (path == null || !path.exists()) {
            return ("Path Doesnt Exist."); // If no such path exists.
        }

        File[] list = path.listFiles();

        if (list != null) {
            for (int i = 0; i < list.length; i++) {

                // Calls if another directory is found.
                if (list[i].isDirectory()) {
                    // System.out.println("Searching Path...");
                    findFile(list[i], target);
                }

                // Block for when desired file is located.
                else if (target.equals(list[i].getName())) {

                  //System.out.println(list[i].getPath());
                    return (list[i].getPath()); // Desired return value, supposed to terminate method and pass value to main.

                }

            }

        }

        return "File Not Found"; // Message if file is not found.

    }

    // Main method to test out return value of method

    public static void main(String[] args) {
        System.out.println(findFile(new File("C:\\Users\\"), "file.exe"));
    }

}

The problem is that you ignore the value returned by the recursive call findFile(list[i], target) . 问题是您忽略了递归调用findFile(list[i], target)返回的值。 When that call finds the target file, you should return the value it returns. 当该调用找到目标文件时,您应该返回它返回的值。

Change: 更改:

        if (list[i].isDirectory()) {
            // System.out.println("Searching Path...");
            findFile(list[i], target);
        }

to: 至:

        if (list[i].isDirectory()) {
            // System.out.println("Searching Path...");
            String result = findFile(list[i], target);
            if (!result.equals("File Not Found") && !result.equals("Path Doesnt Exist.")) {
                return result;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM