0

What I need to do is take a path to search for a file from the user and also take a set of characters from the user which is a part of the filename to be searched. Once the file is found then it would display all the locations including sub-folders along with complete filename.What I have done is taken all the files in the given path and put them in a File[] array using ListFiles().Getting the names of all the files and save them into an arraylist. Make another arraylist and save only the filename without the extension from the previous arraylist. Then use the set of characters entered by the user to compare with each filename in this arraylist.To test if this much was working I printed it out but its printing all the file names and not just the ones that match the character set the user entered.Here is my code:

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

public class FileSearch
 {
  public static void main(String args[])
   {
    try
     {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("  1.Enter the path to search:");
        String str = br.readLine();
        System.out.println("  2.Enter some characters of the filename:");
        String str1 = br.readLine();

        File files = new File(str);

        File[] f = files.listFiles();     

        List<String> li = new ArrayList<String>();
        List<String> li1 = new ArrayList<String>();

        for(int i=0;i<f.length;i++)
        {
            if(f[i].isFile())
            {
                li.add(f[i].getName());
            }
        }

        for(int i=0;i<li.size();i++)
        {
           int j = li.get(i).lastIndexOf('.');
           li1.add((j>-1)?li.get(i).substring(0, j):li.get(i));
        }

        for(int i=0;i<li1.size();i++)
         {
            if(li1.get(i).contains(str1));
            {
                System.out.println(li1.get(i));
            }
        }
     }
    catch(Exception e)
     {
        e.printStackTrace();
     }
  }
 }
Joel
  • 4,732
  • 9
  • 39
  • 54
Robin
  • 147
  • 10
  • Exactly. You are printing out the contents of your li1 arraylist, which holds names without extensions. What are you trying to get it to do? – Joel Jan 14 '13 at 16:42
  • Your code seems to be right to me.. Are you sure of the value of str1? Try walking through with a debugger – Joel Jan 14 '13 at 16:47
  • 2
    just debug it... If you need help: [Eclipse Tutorial](http://www.vogella.com/articles/EclipseDebugging/article.html) – moeTi Jan 14 '13 at 16:53
  • moeTi is right. You need to use a debugger to see the values of all the strings to see what is going wrong. Eclipse has a built in debugger. Put in some break points and start debugging. – Joel Jan 14 '13 at 16:55
  • 1
    Thank you so much...I haven't really ever used the debugger...figured it out :) – Robin Jan 14 '13 at 16:58
  • There's a semi-colon on the end of your conditional `if(li1.get(i).contains(str1));` – Nick Holt Jan 14 '13 at 17:10
  • 1
    As an aside, your code is a bit messy - you could do this logic in a single for-each loop removing the need to create any lists. Also `str `and `str1` are not very good variable names, `searchDirectory` and `filePattern` would be far more indicative of what those variables represent. – Nick Holt Jan 14 '13 at 17:15

2 Answers2

2

Your code is absolutely fine. The following statements is the root cause of the problem.

if(li1.get(i).contains(str1));
{
    System.out.println(li1.get(i));
}

If you have observed clearly you have used ; after the if condition. Remove the semi colon the code would work fine.

Joel
  • 4,732
  • 9
  • 39
  • 54
Patton
  • 2,002
  • 3
  • 25
  • 36
  • 1
    Don't feel bad. That error has gotten everyone at one time or another. If you're using Eclipse you can make such a statement a compiler warning. See http://stackoverflow.com/questions/7571007/compiler-doesnt-complain-when-i-ended-a-line-with-two-semicolons-why – Tansir1 Jan 14 '13 at 17:05
2

Here look at this article. It will help you understand how to debug your program. Looks like you can construct the full path like this:

String fullPath = files.getAbsolutePath() + li1.get(i);
Joel
  • 4,732
  • 9
  • 39
  • 54
  • @JoelThanks a lot :) but now those filenames are stored in an arraylist.I need to print out their complete filepaths.How do I do that since they are strings now. – Robin Jan 14 '13 at 17:19
  • Are you storing the extensions anywhere? if so, you can do a string concatenation to combine them into one string that you can print out – Joel Jan 14 '13 at 17:24
  • 1
    String fullPath = files.getAbsolutePath() + li1.get(i); – Joel Jan 14 '13 at 17:43