tags:

views:

393

answers:

4

I'm currently writing an app for school that is a mini search engine. On execution, it indexes the contents of the text files included as args. I haven't used the try and catch methods before, and we were just given this code in include in our program:

Scanner inputFile = null;
try {
    inputFile = new Scanner(new File("dog.txt"));
} catch (FileNotFoundException fe) {
    System.out.println("File not found!");
}

I've created a method that loops through the args and adds a new object to an array for every unique word found. The problem is that the catch method seems to still execute whenever I run the app, and I can't work out why. This is the output:

dog.txt being indexed ... File not found!
cat.txt being indexed ... File not found!

I've included the method below. If anyone cold maybe point out where I'm going wrong, that would be great.

static void createIndex(String[] args) {
    for(int i = 0; i < args.length; i++) {
        Scanner inputFile = null;
        try {
            System.out.print((args[i]) + " being indexed ... ");
            inputFile = new Scanner(new File(args[i])); 
            while(inputFile.hasNext()) {
                boolean isUnique = true;
                String newWord = inputFile.next().trim().toLowerCase();
                for(int j = 0; j < uniqueWords; j++)
                    if(newWord.equals(wordObjects[j].getWord())) {
                        wordObjects[j].setFile(args[i]);
                        isUnique = false;
                    }

                if(isUnique) {
                    wordObjects[uniqueWords] = new WordIndex(newWord, args[i]);
                    uniqueWords++;
                }
            }

            System.out.print("done");

        } catch(FileNotFoundException fe) {
            System.out.println("File not found!");
        }
    }
}
A: 

The exception is telling you that the files aren't found. So do you know where those .txt files are? Can you see them in the current directory?

Daniel Earwicker
+5  A: 

If you replace the line:

System.out.println("File not found!");

with these lines:

System.out.println("File not found! " + fe);
fe.printStackTrace();

it should tell you exactly what the exception is (and the line number it occurred on).

The next step is to use the full path name in the Scanner constructor (e.g., "/tmp/dog.txt"). It may be that your IDE is running your code from a directory that's different from what you think it is.

You can figure out what actual directory you're in with:

File here = new File (".");
try {
   System.out.println ("Current directory: " + here.getCanonicalPath());
} catch(Exception e) {
    e.printStackTrace();
}
paxdiablo
Thanks pax, I found realised I had the files in the wrong directory, as well as the two lines everyone pointed out below in the wrong order. Thanks for that additional error checking code though. Will definitely come in handy.
Aaron Moodie
A: 

Check to make sure that the file is in the right path (if you're using eclipse, set the run path to the directory of the file before executing.

Otherwise, use the full path name of the file and see if you're still getting this error.

If you moved the line

    System.out.print((args[i]) + " being indexed ... ");

to the line after

    inputFile = new Scanner(new File(args[i]));

I'm almost certain that "file being indexed ..." will not print

Charles Ma
+3  A: 

First thing I'd do is switch around these two lines so you only get the message if the file has actually been found:

        System.out.print((args[i]) + " being indexed ... ");
        inputFile = new Scanner(new File(args[i]));

Basically, put them in like this:

        inputFile = new Scanner(new File(args[i])); 
        System.out.print((args[i]) + " being indexed ... ");

It's less misleading this way IMHO.

Other than that the code looks OK to me, but we're missing out the code for Scanner - I just checked and according to the Java docs (at least for 1.4.2), the File constructor doesn't throw a FileNotFoundException so something else must. Time to get the debugger out and find out exactly where the exception is thrown, or at least get a stack trace that shows you where it's thrown.

Also, you might have to qualify the path of the files you're trying to load if the working directory of your program is not where the files are; From your output it looks like you're not passing any path and you might have to.

Timo Geusch
Thanks Timo, that was it. :)
Aaron Moodie