Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串频率搜索未找到所有单词_Java_String_Text_Frequency_Word Frequency - Fatal编程技术网

Java 字符串频率搜索未找到所有单词

Java 字符串频率搜索未找到所有单词,java,string,text,frequency,word-frequency,Java,String,Text,Frequency,Word Frequency,我正在尝试实现一个字符串频率搜索算法,该算法解析crooks.txt文件,并获取测试中每个唯一单词的出现次数。 该算法应考虑大小写敏感度,使“A”和“A”都是唯一的。到目前为止,该算法似乎跳过了测试中第一次出现的“a”以及以后出现的许多其他单词 此外,words数组包含文本中的每个单词。不知何故,(!isDuplicate)条件中的循环跳过“a”,并且不会增加计数 笑话.txt I wondered why the baseball was getting bigger. Then it hit

我正在尝试实现一个字符串频率搜索算法,该算法解析
crooks.txt
文件,并获取测试中每个唯一单词的出现次数。 该算法应考虑大小写敏感度,使“A”和“A”都是唯一的。到目前为止,该算法似乎跳过了测试中第一次出现的“a”以及以后出现的许多其他单词

此外,
words
数组包含文本中的每个单词。不知何故,
(!isDuplicate)
条件中的循环跳过“a”,并且不会增加
计数

笑话.txt

I wondered why the baseball was getting bigger.
Then it hit me.

Police were called to a day care
where a 3-yr-old was resisting a rest.
...
WordCounter.java

import java.util.*;
import java.io.FileNotFoundException;
import java.io.FileInputStream;

public class WordCounter {
    ArrayList<String> words = new ArrayList<String>();

    //prints number of words in the  file
    public void numOfWords(Scanner key1) {
        int counter = 1;
        while(key1.hasNext()) {
            words.add(key1.next().replaceAll("[^a-zA-Z]", ""));

        }
    }

    //Takes word as parameter and returns frequency of that word
    public void frequencyCounter(Scanner key1) {
        ArrayList <String> freqWords = new ArrayList<String>();
        int count = 1;
        int counter = 1;

        for(int i = 0; i < words.size(); i++){
            boolean isDuplicate = false;
            for (String s: freqWords){
                if (s.contains(words.get(i).trim()))
                    isDuplicate =true;
            }

            if (!isDuplicate){

                for(int j = i + 1; j < words.size(); j++){
                    if(words.get(i).equals(words.get(j))){
                        count++;
                    }
                }
                freqWords.add(count + "-" + words.get(i));
                Collections.sort(freqWords, Collections.reverseOrder());
                count = 1;     
            }
        }

        for(int i = 0; i < freqWords.size(); i++) {
            System.out.print((i+1) + "       ");
            System.out.println(freqWords.get(i));
        }
    }

}
import java.util.*;
导入java.io.FileNotFoundException;
导入java.io.FileInputStream;
公共类字计数器{
ArrayList words=新的ArrayList();
//打印文件中的字数
公共无效数字字(扫描键1){
int计数器=1;
while(key1.hasNext()){
words.add(key1.next().replaceAll(“[^a-zA-Z]”,“);
}
}
//将单词作为参数并返回该单词的频率
公共无效频率计数器(扫描仪键1){
ArrayList freqWords=新的ArrayList();
整数计数=1;
int计数器=1;
for(int i=0;i
请编辑我的错误答案:

但可能是contains()造成了问题,因为API告诉我们它在字符串中搜索Charsequenz。这意味着您基本上是在每个单词中搜索字符序列“a”,并告诉它是重复的。因此,它将“天”计算为1,因为您正在搜索“a”


在我看来,使用HashMap搜索重复项会更好,而且会更快。您可以计算值中有多少个值。

只需编辑我的错误答案:

但可能是contains()造成了问题,因为API告诉我们它在字符串中搜索字符序列。这意味着您基本上是在每个单词中搜索字符序列“a”,并告诉它是重复的。因此,由于您在搜索“a”,所以它将“天”计算为一天


在我看来,使用HashMap搜索重复项会更好,而且会更快。而且您可以计算值中有多少个值。

您确定重复项的逻辑有点不正确:

        boolean isDuplicate = false;
        for (String s: freqWords){
            if (s.contains(words.get(i).trim()))
                isDuplicate =true;
        }

这将使isDuplicate为true if words.get(i)为“a”,s为“apple”,因为apple包含“a”。检查s中的单词是否与words.get(i)完全匹配。

您确定重复项的逻辑有点不正确:

        boolean isDuplicate = false;
        for (String s: freqWords){
            if (s.contains(words.get(i).trim()))
                isDuplicate =true;
        }

如果单词。get(i)是“a”,s是“apple”,因为apple包含“a”。检查s中的单词是否与单词匹配。get(i)准确。

请更具体一些。@shmosel只是做了,直到不清楚问题出在哪里。告诉我们实际发生了什么,而不是你想象的代码在做什么。@shmosel我运行调试程序来跟踪发生了什么,跳过了“a”。我不知道是什么原因。跳过了哪里?什么时候?从什么开始?请保持沉默e具体。@shmosel只是做了一些事情,直到不清楚问题出在哪里。告诉我们实际发生了什么,而不是你想象的代码在做什么。@shmosel我运行调试程序来跟踪发生了什么,跳过了“a”。我不知道是什么原因。跳过了哪里?何时?从什么开始?
^
是否定。编辑为w我认为您正在搜索的解决方案是否定的