【问题标题】:Java - My file cannot be read and i cannot figure this outJava - 我的文件无法读取,我无法弄清楚
【发布时间】:2023-03-15 18:05:01
【问题描述】:

我目前正在尝试编写此代码,但无法理解为什么我的文件 common-dictionary.txt 无法读入。它具有简单的名称,例如“aaron”和“address”,但主要问题是它只是没有检测到它。即使它存在于文件common-dictionary.txt 中,它也总是以“在字典中找不到该单词”结尾。任何帮助将不胜感激。

这是目前为止的代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;

public class Project_12 {

    public static void main(String[] args) throws FileNotFoundException, IOException {

        String prompt = "Enter a word or 'quit' to stop: ";

        ArrayList<String> personalDictionary = new ArrayList<String>();
        ArrayList<String> commonDictionary = new ArrayList<String>();

        // Construct a Scanner to read user input from the keyboard.
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Spell Checker");
        System.out.println("-------------");

        // Perform a priming read to get the first word.
        System.out.print(prompt);
        String word = keyboard.nextLine().toLowerCase();

        // Enter the user input loop.
        while (!word.equals("quit")) {

            // Check if the word is in either dictionary.
            if (checkSpelling(word, personalDictionary, commonDictionary)) {
                System.out.println("The word is spelled correctly.");
            } 
            else {
                System.out.println("The word was not found in the dictionary.");            
                System.out.println("Would you like to add it to your personal dictionary (yes/no)?");
                String response = keyboard.nextLine().toLowerCase();

                if (response.equalsIgnoreCase("yes")) {
                    word.toLowerCase();
                    personalDictionary.add(word);
                    Collections.sort(personalDictionary);
                    System.out.println("Word added. Enter a word to 'quit' to stop");
                }
            }

            // Get the next word from the user.
            System.out.println();
            System.out.print(prompt);
            word = keyboard.nextLine().toLowerCase();
        }

        keyboard.close();
        System.out.println("Goodbye!");
    }

    public static ArrayList<String> readFile() throws FileNotFoundException {

            Scanner scan = new Scanner(new File("common-dictionary.txt"));
            ArrayList<String> commonFile = new ArrayList<String>();

            while(scan.hasNextLine());
            {
                commonFile.add(scan.nextLine());
            }
            scan.close();
            return commonFile;
        }

    // Return true if word is in either array; otherwise, return false. Note 
    // that the arrays are sorted, so binary search can be used.
    public static boolean checkSpelling(String word, ArrayList<String> personal, ArrayList<String> common) {

    if (Collections.binarySearch(common, word.toLowerCase()) >= 0) {
        return true;
    }
    if (Collections.binarySearch(personal, word.toLowerCase()) >= 0) {
        return true;
    }
        return false;
    }


    // Write the nonempty elements of an oversize array to a given file.
    public static void writeFile(ArrayList<String> personal)
            throws FileNotFoundException {

        PrintWriter writer = new PrintWriter("personal-dictionary.txt");
        Collections.sort(personal);

        int length = personal.size();

        for (int i = 0; i < length; ++i) {
            writer.write(personal.get(i));
        }

        // Close the file; otherwise, the contents will be lost.
        writer.close();
    }
}

【问题讨论】:

  • Scanner scan = new Scanner(new File("common-dictionary.txt")); 将假定文件位于“当前工作目录”中,这很重要,因为这可能不是“当前执行/安装目录”。使用 System.out.println(new File("common-dictionary.txt").exists()) 之类的东西来确定文件是否真的可以找到
  • 也可以使用System.out.println(new File(".").getAbsolutePath())打印工作目录
  • 我没有看到你在 main 方法中调用 readFile,好像你只是没有将字典加载到程序中。
  • @MichaelMurray 我应该在哪里添加 readFile 方法?当我插入它时,它似乎与输出混淆了。
  • 根据您的代码,您似乎需要调用它来返回 commonDictionary 的值,例如 List&lt;String&gt; commonDictionary = readFile();

标签: java file arraylist


【解决方案1】:

checkSpelling 方法总是返回 false,因为 ArrayList&lt;String&gt; commonDictionary 被初始化为空列表:ArrayList&lt;String&gt; commonDictionary = new ArrayList&lt;String&gt;();。为了将文件的内容放入commonDictionary,您必须将其设置为方法readFile()返回的列表,其中包含存储在文件中的单词:ArrayList&lt;String&gt; commonDictionary = readFile();

【讨论】:

  • 所以我将 readFile 中的 commonDictionary 变量更改为“commonFile”,因为它变得令人困惑,基于此信息,您所说的 是什么意思?“您必须将其设置为列表由 readFile() 方法返回,其中包含存储在文件中的单词: ArrayList commonDictionary = readFile();" 这让我很难过,我真的不知道该怎么办。
  • readFile() 方法读取文件的行并将它们放入列表中。该列表由方法返回。为了获得这个列表,您必须调用该方法并将其分配给一个变量。使用ArrayList&lt;String&gt; commonDictionary = readFile();,列表commonDictionary 被赋予包含文件行的列表。
  • 当我将ArrayList&lt;String&gt; commonDictionary = new ArrayList&lt;String&gt;(); 替换为ArrayList&lt;String&gt; commonDictionary = readFile(); 时,它的输出仅为“真”,我无法找出问题所在,因为我看不出是什么导致没有其他问题出现
  • 什么返回true?方法checkSpelling?
  • 当我运行程序时,它的唯一输出是“true”,我无法弄清楚它来自哪里或导致它的原因
猜你喜欢
  • 2016-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-15
  • 2011-04-20
  • 1970-01-01
  • 2021-06-04
相关资源
最近更新 更多