gpt4 book ai didi

java - 删除以前的字符串条目

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

不太确定如何表达这个问题。我正在编写一个拼写检查程序,它从文本文件中读取单词。然后将这些单词添加到以下类型的 ArrayList 中:

ArrayList<String> dictionaryList = new ArrayList<String>();

现在,程序以无限循环的方式运行(直到键入“q”),并提示用户搜索单词。如果可以找到该单词,程序将返回一条简单的“找到单词”消息。

否则;获取搜索词并生成该词的变体。这些变体已添加到列表中:

ArrayList<String> allSimilarWords = new ArrayList<String>();

例如,单词“coffei”会生成一系列“相似单词”,其中之一是“coffee”(英语单词) 。

现在,看到我既有所有字典单词的列表,也有相似单词的列表,程序会检查 allSimilarWords 中的单词是否包含在 dictionaryList< 中/强>。

我已经检查了我的 generateSimilarWords 方法,它按预期工作。

但是,在第二次要求用户输入时出现问题。第一次(输入“coffei”(其中 coffee 在字典列表中)),程序会生成以下输出:

Enter search term: 
coffei
Suggestion for similar words:
-coffee

由于这是一个无限循环,程序要求我输入另一个单词。现在,如果我输入“banaan”(其中 banana 在字典中),程序会生成以下输出:

Enter search term: 
banaan
Suggestion for similar words:
-coffee
-banana

因此,它“记住”我搜索咖啡,并除了“香蕉”一词之外还提供“咖啡”作为建议词。我一直试图找出为什么会发生这种情况,但找不到我的错误。任何帮助将不胜感激。代码如下:

代码

//GETTING SIMILAR WORD ARRAYLIST
public ArrayList<ArrayList<String>> getSimilarWords(String searchWord) {
this.searchWord = searchWord;
char[] wordAsCharacterArray = searchWord.toCharArray(); //Convert search String to a character array

//Call helper methods to add the suggested similar words to the similarWordArrayList
similarWordArrayList.add(charSwap(wordAsCharacterArray)); //Add swapped character suggestions
similarWordArrayList.add(charRemove(wordAsCharacterArray)); //Add removed character suggestions
similarWordArrayList.add(charAdd(wordAsCharacterArray)); //Add removed character suggestions
similarWordArrayList.add(charReplace(wordAsCharacterArray)); //Add removed character suggestions

return similarWordArrayList;
}

similarWordArrayList 随后被“转换”为仅包含 String 元素的 ArrayList,而不是 ArrayList < ArrayList< String >> 类型的列表。

//Method to generate and suggest similar words
private void suggestWords(ArrayList<ArrayList<String>> similarWords) {
allSimilarWords = convertSingleList(similarWords);
String currWord = "";

outer: for(int i = 0; i < allSimilarWords.size(); i++) { //Iterate through the list of similar words
currWord = allSimilarWords.get(i); //Set current similar word to a String variable
for(int j = 0; j < i; j++) {
if(currWord.equalsIgnoreCase(allSimilarWords.get(j))) {
continue outer;
}
}
if(myDictionaryList.contains(currWord)) {
System.out.println("-" + currWord);
}

}
}

查找字符串的方法:

public void findString(String word) {
searchTerm = word;
if(myDictionaryList.contains(word)) { //If found
System.out.println("Found word!"); //Notify
}
else {
//Find all similar words
System.out.println("Suggestions for similar words: ");
similarWords = handler.getSimilarWords(searchTerm);
suggestWords(similarWords);
}
}

任何有关如何解决此问题的帮助,以便程序仅根据最后一个搜索词打印出建议的单词(而不记住以前建议的单词),我们非常感激。

最佳答案

我认为,您要做的第一件事是在向数组列表添加单词之前清除数组列表。

//GETTING SIMILAR WORD ARRAYLIST
public ArrayList<ArrayList<String>> getSimilarWords(String searchWord) {
this.searchWord = searchWord;
char[] wordAsCharacterArray = searchWord.toCharArray(); //Convert search String to a character array

//Clear the list before adding words
similarWordArrayList.clear();

//Call helper methods to add the suggested similar words to the similarWordArrayList
similarWordArrayList.add(charSwap(wordAsCharacterArray)); //Add swapped character suggestions
similarWordArrayList.add(charRemove(wordAsCharacterArray)); //Add removed character suggestions
similarWordArrayList.add(charAdd(wordAsCharacterArray)); //Add removed character suggestions
similarWordArrayList.add(charReplace(wordAsCharacterArray)); //Add removed character suggestions

return similarWordArrayList;
}

关于java - 删除以前的字符串条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26138042/

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