return语句在两个循环内被跳过。

huangapple go评论70阅读模式
英文:

return statement being skipped inside 2 loops

问题

以下是您要求的翻译内容:

我正在使用在网上找到并进行了修改的以下方法,以在给定目录中仅使用文件名查找文件:

public static void main(String[] args)
{
   Scanner scan = new Scanner(System.in);
   System.out.println("输入要查找的文件名:");
   String name = scan.next();
   System.out.println("输入要搜索的目录:");
   String directory = scan.next();
   System.out.println(findFile(name,new File(directory)));
}

public static String findFile(String name,File file)
{
    String correctFilePath = null;
    ArrayList<String> possiblePaths = new ArrayList<String>();
    File[] list = file.listFiles();
    String nameToFind = "anyname";
    if(list!=null)
        for (File fil : list)
        {
            if (fil.isDirectory())
            {
                findFile(name,fil);
            } else if (name.equalsIgnoreCase(fil.getName()))
            {
                possiblePaths.add(fil.getParent());
            }
        }
    // 此处,已将所有包含我正在查找的文件名的文件添加到了ArrayList中。
    // 但是,我知道路径的结尾是什么,
    // 这是我在以下循环中检查的
    String filePath;
    for (int i = 0; i < possiblePaths.size(); i++)
    {
        filePath = possiblePaths.get(i);
        for (int j = 0; j < nameToFind.length(); j++)
        {
            if (filePath.charAt(filePath.length()     - (j + 1)) ==
                nameToFind.charAt(nameToFind.length() - (j + 1))    )
            {
                if (j == nameToFind.length() - 1)
                {
                    correctFilePath = filePath;
                    return correctFilePath;
                }
            }
        }
    }
    return correctFilePath;
}

目前的情况是,一旦找到文件并执行了correctFilePath = filePath,返回语句将被跳过,并且我被带回到 i 循环的开头,correctFilePath = null。我该如何修复这个问题?

英文:

I am using the following method that i found online and tweaked to find files in a given directory using only the file's name:

public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println(&quot;Enter the file to be searched.. &quot; );
String name = scan.next();
System.out.println(&quot;Enter the directory where to search &quot;);
String directory = scan.next();
System.out.println(findFile(name,new File(directory)));
}
public static String findFile(String name,File file)
{
String correctFilePath = null;
ArrayList&lt;String&gt; possiblePaths = new ArrayList&lt;String&gt;();
File[] list = file.listFiles();
String nameToFind = &quot;anyname&quot;;
if(list!=null)
for (File fil : list)
{
if (fil.isDirectory())
{
findFile(name,fil);
} else if (name.equalsIgnoreCase(fil.getName()))
{
possiblePaths.add(fil.getParent());
}
}
// Here, all the files containing the name of the file that I am looking
// for have been already added to the ArrayList.
// However, I know what the ending of the path is,
// which is what I am checking in the following loops
String filePath;
for (int i = 0; i &lt; possiblePaths.size(); i++)
{
filePath = possiblePaths.get(i);
for (int j = 0; j &lt; nameToFind.length(); j++)
{
if (filePath.charAt(filePath.length()     - (j + 1)) ==
nameToFind.charAt(nameToFind.length() - (j + 1))    )
{
if (j == nameToFind.length() - 1)
{
correctFilePath = filePath;
return correctFilePath;
}
}
}
}
return correctFilePath;
}

Right now, what happens is that once the file is found and correctFilePath = filePath is executed, the return statement is being skipped, and I am brought back to the beginning of the i loop, with correctFilePath = null. How could i fix this?

答案1

得分: 1

因此,最终我移除了那个return语句和correctFilePath = filePath;语句,并且用所需字符串在ArrayList内的位置的指示进行了替代,该位置我设置为静态的。

英文:

So in the end, i took away that return statement and the correctFilePath = filePath; statement, and replaced both by an indication of the desired String's position inside of the ArrayList, which i set to static.

huangapple
  • 本文由 发表于 2020年9月25日 05:50:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64054911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定