检查单词是否在文本文件中

时间:2017-02-20 00:49:15

标签: java arrays dictionary java.util.scanner

我正在尝试制作一个程序来检查一串字母是否是一个文本文件,对于一个字典类型的程序,但我无法让它工作。我的代码是:

package main;

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

public class Test {
public static void main(String [] args) {
    Scanner Enter = new Scanner(System.in);


    System.out.println("Please Enter your word");
    String Guess = Enter.next();

    // The name of the file to open.
    String fileName = "../Dictionary/Resources/wordsA.txt";

    // This will reference one line at a time
    String line = null;

    int i = 0;
    int numWords = 0;

    String[] wordArray;
    wordArray = new String[15000];


    try {
        // FileReader reads text files in the default encoding.
        FileReader fileReader = 
            new FileReader(fileName);

        // Always wrap FileReader in BufferedReader.
        BufferedReader bufferedReader = 
            new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null) {
            numWords++;

            wordArray[numWords] = line;
        }

        // Always close files.
        bufferedReader.close();         
    }
    catch(FileNotFoundException ex) {
        System.out.println(
            "Unable to open file '" + 
            fileName + "'");                
    }
    catch(IOException ex) {
        System.out.println(
            "Error reading file '" 
            + fileName + "'");                  
        // Or we could just do this: 
        // ex.printStackTrace();
    }
    for(i = 0; i < 10; i++){
        System.out.println(wordArray[i]);
        if(Guess == wordArray[i]){
            System.out.println("In dictionary");
        }
        else{
            System.out.println("Not in Dictionary");
        }
    }
}
}

它运行我的文本文件并打印文件中的前10个单词,并显示我的单词是否在文本文件字典中。从下面的输出中可以看出,我没有得到的是,我输入了一个像“AA”这样的词,它仍然没有在字典中说,但应该说它是?我无法弄清楚原因。

示例输出:

Please Enter your word
AA
null
Not in Dictionary
AA
Not in Dictionary
AAH
Not in Dictionary
AAHED
Not in Dictionary
AAHING
Not in Dictionary
AAHS
Not in Dictionary
AAL
Not in Dictionary
AALII
Not in Dictionary
AALIIS
Not in Dictionary
AALS
Not in Dictionary

0 个答案:

没有答案