gpt4 book ai didi

Java - 我的文件无法读取,我无法弄清楚这一点

转载 作者:行者123 更新时间:2023-12-01 17:15:28 25 4
gpt4 key购买 nike

我目前正在尝试编写此代码,但无法理解为什么我的文件 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();
}
}

最佳答案

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

关于Java - 我的文件无法读取,我无法弄清楚这一点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61400168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com