猿问

Java中的拼写检查器,文件打开但没有输出

我得到了这个拼写检查器的代码。它不读取 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(", ");

     

慕田峪4524236
浏览 122回答 2
2回答

阿晨1998

&nbsp;private void getInputFileNameFromUser(java.awt.event.ActionEvent evt) {&nbsp; &nbsp; JFileChooser chooser = new JFileChooser();&nbsp; &nbsp; FileNameExtensionFilter filter = new FileNameExtensionFilter(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Text Files", "txt");&nbsp; &nbsp; chooser.setFileFilter(filter);&nbsp; &nbsp; int returnVal = chooser.showOpenDialog(null);&nbsp; &nbsp; if(returnVal == JFileChooser.APPROVE_OPTION) {&nbsp; &nbsp; &nbsp; &nbsp; tfLogFile.setText(chooser.getSelectedFile().getPath());&nbsp; &nbsp; }}其中 tfLogFile 是存储输出的文件
随时随地看视频慕课网APP

相关分类

Java
我要回答