我得到了这个拼写检查器的代码。它不读取 words.txt 文件。它只打开对话框来选择一个文件。当我选择 words.txt 文件时,对话框关闭,没有任何反应。
我不确定这段代码有什么问题。我一直在检查它,一切似乎都到位了。有人可以指出我哪里出错了吗?
谢谢你。
import java.io.*;
import java.util.Scanner;
import java.util.HashSet;
import javax.swing.*;
import java.util.TreeSet;
/**
* This class works as a basic spell-checker. It uses the file words.txt to
* check whether a given word is correctly spelled.
*/
public class SpellChecker {
public static void main(String[] args) {
Scanner words;
HashSet<String> dict = new HashSet<String>();
Scanner userFile;
try {
words = new Scanner(new File("src/words.txt"));
while (words.hasNext()) {
String word = words.next();
dict.add(word.toLowerCase());
}
userFile = new Scanner(getInputFileNameFromUser());
// Skip over any non-letter characters in the file.
userFile.useDelimiter("[^a-zA-Z]+");
HashSet<String> badWords = new HashSet<String>();
while (userFile.hasNext()) {
String userWord = userFile.next();
userWord = userWord.toLowerCase();
if (!dict.contains(userWord) &&
!badWords.contains(userWord)) {
badWords.add(userWord);
TreeSet<String> goodWords = new TreeSet<String>();
goodWords = corrections(userWord, dict);
System.out.print(userWord + ": ");
if (goodWords.isEmpty())
System.out.println("(no suggestions)");
else {
int count = 0;
for (String goodWord: goodWords) {
System.out.print(goodWord);
if (count < goodWords.size() - 1)
System.out.print(", ");
阿晨1998
相关分类