Java 字数法

Java 字数法,java,bluej,word-count,Java,Bluej,Word Count,我不明白为什么我的代码不起作用。我得到一个错误:“找不到在数组类中定义的符号-方法getLength(),”。关于如何改进这种方法有什么建议吗?谢谢大家! /** * getWordCount * * Get a count of how many times each word occurs in an input String. * * @param text a string containing the text you wish t

我不明白为什么我的代码不起作用。我得到一个错误:“找不到在数组类中定义的符号-方法getLength(),”。关于如何改进这种方法有什么建议吗?谢谢大家!

/**
     * getWordCount
     * 
     * Get a count of how many times each word occurs in an input String.
     * 
     * @param text a string containing the text you wish to analyze
     * @return a map containing entries whose keys are words, and
     *         whose values correspond to the number of times that word occurs
     *         in the input String text.
     */
    public Map<String,Integer> getWordCount(String text)
    {
         String[] parts = text.trim().split("('s|\\W)+"); 

        Map<String, Integer> wordCountMap = new TreeMap<String, Integer>();
        for(int i=0;i<parts.getLength();i++)
        {
            for(String text : parts[i].toString())
            {
                if(!wordCountMap.containsKey(text))
                {
                     wordCountMap.put(text,1);
                } else {
                    int freq = wordCountMap.get(text);
                    freq++;
                    wordCountMap.put(text,freq);
                }
                return wordCountMap;
            }
            return new TreeMap<String,Integer>();
        } 
    }
/**
*getWordCount
* 
*获取每个单词在输入字符串中出现的次数计数。
* 
*@param text包含要分析的文本的字符串
*@返回包含关键字为单词的条目的地图,以及
*其值对应于该单词出现的次数
*在输入字符串文本中。
*/
公共映射getWordCount(字符串文本)
{
String[]parts=text.trim().split(“('s |\\W)+”);
Map wordCountMap=newtreemap();

对于(inti=0;i,您的代码存在不止一个问题

Map<String, Integer> map = getWordCount(" the fox jumped over the moon ");
System.out.println("size:" + map.size());
以下更改可能会有所帮助

public Map<String,Integer> getWordCount(String text)
{
    String[] parts = text.trim().split("('s|\\W)+"); 

    Map<String, Integer> wordCountMap = new TreeMap<String, Integer>();
    for (String part : parts)
    {
        if(!wordCountMap.containsKey(part))
        {
             wordCountMap.put(part,1);
        } else {
            int freq = wordCountMap.get(part);
            freq++;
            wordCountMap.put(part,freq);
        }
    }
    return wordCountMap;
}
publicmap getWordCount(字符串文本)
{
String[]parts=text.trim().split(“('s |\\W)+”);
Map wordCountMap=newtreemap();
用于(字符串部分:部分)
{
如果(!wordCountMap.containsKey(部分))
{
wordCountMap.put(第1部分);
}否则{
int freq=wordCountMap.get(部分);
freq++;
wordCountMap.put(part,freq);
}
}
返回wordCountMap;
}
下面的测试代码给出了5的大小,这似乎证实了您希望在代码中执行的操作

Map<String, Integer> map = getWordCount(" the fox jumped over the moon ");
System.out.println("size:" + map.size());
Map Map=getWordCount(“狐狸跳过了月球”);
System.out.println(“size:+map.size());

A
String[]
不是
java.lang.reflect.Array
.for(int i=0;我希望你@SotiriosDelimanolis!这就解决了问题。@SotiriosDelimanolis还有,我如何迭代数组中的特定单词(String)然后返回该字符串出现的频率?非常感谢!这非常有帮助。我不知道如何迭代数组并使其使用该字符串。