-1

I have been working on a small program and I'm new to Java. But it keeps raising filenotfound exception. Here's my code:

import java.io.FileNotFoundException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/* 
 * Don't forget your file header comment here!
 */

public class WordSearch {
    /*
     * This is how you declare constants in Java. You can now type
     * 'MIN_WORD_LENGTH' anywhere you want to refer to it.
     */
    private static final int MIN_WORD_LENGTH = 3;

    public static void main(String[] args) throws FileNotFoundException {
        // Remember, to access command-line arguments, you use args[0],
        // args[1],...
        // See CommandLine.java and Stdin.java in the Class Examples github for examples.
        List<String> dictList= new ArrayList<>();
        dictList = dictRead(args[0]);
    }
    public static List<String> dictRead (String filePath) throws FileNotFoundException{
        Scanner dictScan = null;
        dictScan = new Scanner(new File(filePath));
        List<String> dictList = new ArrayList<>();
        int i=0;
        while (dictScan.hasNext()) {
            dictList.add(i, dictScan.nextLine());
            i+=1;
        }
        return dictList;
    }

}

This I don't know why I keep getting this exception. I changed my run configuration and put TestCases/dictionary.txt as my first argument.

Here's a picture of my directory and I'm running WordSearch.java:

enter image description here

3 Answers3

1

Your folder structure has project within project. In you IDE, if you have only PA1-WordSearch imported as a project, it will work.

Niyas
  • 229
  • 1
  • 7
0

I hope it will work for you

dictList = dictRead("./FileFolder/aaa.txt");

Shinky
  • 1
  • 3
0

The file is not in the place where Java expects it.

TestCases/dictionary.txt is a relative path. To find the file on your drive, it gets interpreted relative to some "working directory". You can probably find that in the run configuration.

But it's more robust to specify the file with an absolute path, one that begins with C:\ or similar if you are on a Windows machine. Using the absolute path makes you independent of the working directory.

So: find the absolute path of your file using the Windows Explorer, and insert that path as the argument in your run configuration.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7