1

My objective is to eventually make a spell checker but I need a dictionary of words to do that.

Here I'm trying to allow the user to input any number of text files as long as there's a space in between the file names ("novel1.txt novel2.txt novel3.txt").

I will use every word from these novels to write to a .dat file of individual words on individual lines(i.e. a dictionary of words). However I'm getting a file not found error at Scanner read = new Scanner(new File(filenames[i])); even though I know that I have the file.

I have even tried putting it in the source package to make sure it could be found.

At the very bottom of my code is a small test I ran (commenting out the other code first) and it does indeed print "war.txt isn't a file," even though I can clearly see that I have the txt file and have typed it correctly.

Can somebody tell me why java isn't seeing my txt file or maybe doesn't think it is a normal file?

public static void main(String[] args) throws FileNotFoundException {

    Scanner in = new Scanner(System.in);

    System.out.println("Please enter the file names exactly.");

    String userInput = in.nextLine();
    String[] filenames = userInput.split(" "); // turning user input string into a string array so I can look at each string individually

    // takes each individual string from filenames and turns each one into the file
    // that the string should represent then adds the file's contents to my dictionary
    for(int i = 0; i < filenames.length; i++){
        Scanner read = new Scanner(new File(filenames[i]));
        String word = null;
        while(read.hasNext()){
            if(read.next().length() >= 2){
                word = read.next();
                // write word into myDict.dat
            }
            System.out.println(word);
        }
    }
    File war = new File("war.txt");
    if(!war.isFile()){
        System.out.println(war + " isn't a file.");
    }
}
user6904265
  • 1,938
  • 1
  • 16
  • 21
Cody Winter
  • 11
  • 1
  • 4
  • How do you run your code? In which directory are your `class` and your `txt` files? Are there in the same? – SubOptimal Nov 19 '16 at 11:20
  • Just an input suggestion: Try reading lines instead until a terminal line, e.g. `"end"`, it'll make files with spaces etc easier to input (you could go crazier and do things like substringing and quotes/parsing etc, but this would be a simpler fix) – Rogue Nov 19 '16 at 11:21
  • @SubOptimal I have put the txt files in the same directory as my program. – Cody Winter Nov 19 '16 at 11:22

2 Answers2

0

I believe you do something in a wrong way. Try following example and compare it with your actual file locations.

Demo.java

import java.io.*;
class Demo {
    public static void main(String[] args) throws IOException {
        File war = new File("war.txt");
        if(!war.isFile()){
            System.out.println(war + " isn't a file.");
        } else {
            System.out.println(war + " is a file.");
        }
    }
}

compile and run it

javac Demo.java
java Demo

output

war.txt isn't a file.

now create in the same directory the war.txt

echo "foobar" > war.txt

run the code again

java Demo

output

war.txt is a file.
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • I do not get the same output. war.txt is in the same directory as my demo class. – Cody Winter Nov 19 '16 at 11:38
  • @CodyWinter have you executed the example in the same way as it was mentioned in the answer? Then you should get the same result. Othwerise add to your question which files are in which directory. In which directory do you execute which command. – SubOptimal Nov 21 '16 at 11:11
0

For the FileNotFoundException make sure that files are in your classpath if you insert only the filenames (for example if you use eclipse put the files on the root folder of the project).

For the war.txt issue you should do this:

File war = new File("war.txt");
if (!war.exists()) {
    war.createNewFile();
}
if(!war.isFile()){
    System.out.println(war + " isn't a file.");
}

This because when you do File war = new File("war.txt"); you are not creating the file, you have to explicitily create it with war.createNewFile();.

Finally, pay attention here:

if(read.next().length() >= 2){
    word = read.next();
    // write word into myDict.dat
}
System.out.println(word);

You do two times read.next() without check read.hasNext() the second time. You should write something like that:

while(read.hasNext()){
    String next = read.next();
    if(next.length() >= 2){
        word = next;
        // write word into myDict.dat
    }
    System.out.println(word);
}
user6904265
  • 1,938
  • 1
  • 16
  • 21
  • I don't want to create a file "war.txt." The file already exists. I'm trying to collect the words from an existing war.txt file. Am I misunderstanding what the method .createNewFile(); does? – Cody Winter Nov 19 '16 at 13:28
  • ok, my bad, I did not realize what the war.txt file was.. I think you have only a classpath problem.. If you use eclipse you should put your file on the root of your project. If you run your program with java command you should add the classpath option with the folder where your files are placed. – user6904265 Nov 19 '16 at 13:44
  • Thanks for the advice, but I use netbeans. I've tried placing the txt files i'm using in the source package along with my source code, but it still won't work. – Cody Winter Nov 19 '16 at 22:47
  • ok, did you try to place the .txt files at the root of your netbeans project? – user6904265 Nov 19 '16 at 23:23
  • I'm not understanding what you mean by the 'root' of my project. I'm pretty inexperienced. – Cody Winter Nov 20 '16 at 00:48
  • I think [this](http://stackoverflow.com/questions/7091898/netbeans-filereader-filenotfound-exception-when-the-file-is-in-folder) will be helpful to you. – user6904265 Nov 20 '16 at 01:01