我得到了这个拼写检查器的代码。它不读取 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(", ");
2 回答
![?](http://img1.sycdn.imooc.com/545861f00001be3402200220-100-100.jpg)
阿晨1998
TA贡献2037条经验 获得超6个赞
private void getInputFileNameFromUser(java.awt.event.ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Text Files", "txt");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
tfLogFile.setText(chooser.getSelectedFile().getPath());
}
}
其中 tfLogFile 是存储输出的文件
添加回答
举报
0/150
提交
取消