简体   繁体   中英

reading txt file and writing a dictionary

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(ie 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.");
    }
}

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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM