Java 你没办法数到字数吗?

Java 你没办法数到字数吗?,java,Java,基本上,我是想知道我已经添加到arraylist中的单词的词数 (使用addWord方法)。起初我尝试使用streams,但我放弃了,现在我尝试使用一个相对简单的迭代器方法。我只是不能让它工作(我总是得到0)。任何帮助都将不胜感激 主要方法 import java.util.List; /** * A simple 'driver' for the WordAnalyser class. * * Split a 'document' into individual words and

基本上,我是想知道我已经添加到arraylist中的单词的词数 (使用addWord方法)。起初我尝试使用streams,但我放弃了,现在我尝试使用一个相对简单的迭代器方法。我只是不能让它工作(我总是得到0)。任何帮助都将不胜感激

主要方法

import java.util.List;

/**
 * A simple 'driver' for the WordAnalyser class.
 * 
 * Split a 'document' into individual words and pass them
 * to a WordAnalyser to be analysed.
 * 
 * In this example, the 'document' is a String literal in
 * the main method, but this could easily be changed to an
 * external file, if desired (e.g., use a Scanner).
 * 
 * Print a few sample statistics.
 *
 */
public class WordAnalyserMain
{
    public static void main(String[] args)
    {
        String document = 
            "The first day of the third month was always difficult for Joy because there was little to look forward to in the month of March.";
        String[] words = document.split("[ ,.;:?]+");
        WordAnalyser analyser = new WordAnalyser();
        for(String word : words) {
            analyser.addWord(word);
        }
        for(String word : List.of("The", "the", "joy", "was")) {
            System.out.println(word);
            System.out.println("    " + analyser.getCount(word));
            System.out.println("    " + analyser.getCaseInsensitiveCount(word));
            System.out.println("    " + analyser.followedBy(word, "month"));
        }
    }
}

到目前为止,我的语文课还没有结束


import java.util.*;
/**
 * Keep track of word counts and word pairs.
 *
 * @author 
 * @version 
 */
public class WordAnalyser
{    
    private HashMap<String,Integer> counts;
    private ArrayList<String> WordAnalyser;
    private String wordCounts;
    private String Sentences;
    private int count;
    private Map<String, Integer> wordCount = new HashMap<String, Integer>();

    /**
     * Constructor for objects of class WordAnalyser
     */
    public WordAnalyser()
    {
        counts = new HashMap<>();
        WordAnalyser = new ArrayList<>();
    }

    /**
     * Uses a for each loop to add the input, starting from 0 
     */
    public void addWord(String input)
    {
        WordAnalyser.add(input);
        this.counts = counts;
        for (String word : WordAnalyser)
        {
            int counter = counts.getOrDefault(word , 0);
            counts.put(word,counter +1);
            System.out.println(input);

        }
    }

    /**
     * Get the number of times the given word has been seen.
     * @param word The word to be looked up.
     * @return The number of times the word has been seen.
     */

    public int getCount(String word)
    {
        Map<String, Integer> wordCount = new HashMap<String, Integer>();
        this.count=count;
        for(String words: WordAnalyser) {
            Integer count = wordCount.get(words);          
            wordCount.put(words, (count== null) ? 1 : count+1);

        }
        return count;
    }

}


导入java.util.*;
/**
*记录单词数量和单词对。
*
*@作者
*@版本
*/
公共类字分析器
{    
私有哈希映射计数;
私有ArrayList词汇分析器;
私有字符串字数;
私人串句;
私人整数计数;
private-Map-wordCount=new-HashMap();
/**
*WordAnalyzer类对象的构造函数
*/
公共词汇分析器()
{
计数=新的HashMap();
WordAnalyzer=newArrayList();
}
/**
*使用for each循环添加输入,从0开始
*/
公共void addWord(字符串输入)
{
添加(输入);
这个。计数=计数;
for(字符串字:WordAnalyzer)
{
int counter=counts.getOrDefault(字,0);
计数。输入(字、计数器+1);
系统输出打印项次(输入);
}
}
/**
*获取给定单词被看到的次数。
*@param word要查找的单词。
*@return该单词被看到的次数。
*/
public int getCount(字符串字)
{
Map wordCount=new HashMap();
这个.count=count;
for(字符串:WordAnalyzer){
整数计数=字计数。获取(字);
put(单词,(count==null)?1:count+1;
}
返回计数;
}
}

如果我理解正确:

private Map<String, Integer> save;

public void addWord(String word) {
    if(save.containsKey(word)) {
        save.put(word, save.get(word)+1);
    } else {
        save.put(word, 1);
    }
}

public int getCount(String word) {
    return save.containsKey(word) ? save.get(word) : 0;
}
私有地图保存;
公共无效添加字(字符串字){
if(save.containsKey(word)){
save.put(单词,save.get(单词)+1);
}否则{
save.put(word,1);
}
}
public int getCount(字符串字){
返回save.containsKey(word)?save.get(word):0;
}
将代码更改为

public class WordAnalyser {
    private ArrayList<String> WordAnalyser;
    private String wordCounts;
    private String Sentences;
    private int count;
    private Map<String, Integer> wordCount = new HashMap<String, Integer>();

    /**
     * Constructor for objects of class WordAnalyser
     */
    public WordAnalyser() {
        wordCount = new HashMap<>();
        WordAnalyser = new ArrayList<>();
    }

    /**
     * Uses a for each loop to add the input, starting from 0
     */
    public void addWord(String input) {
        WordAnalyser.add(input);
        int counter = wordCount.getOrDefault(input, 0);
        wordCount.put(input, counter + 1);
        System.out.println(input);
    }

    /**
     * Get the number of times the given word has been seen.
     *
     * @param word The word to be looked up.
     * @return The number of times the word has been seen.
     */

    public int getCount(String word) {
        Integer count = wordCount.get(word);
        if ( count == null ) {
            return 0;
        }
        return count;
    }
}
公共类字分析器{
私有ArrayList词汇分析器;
私有字符串字数;
私人串句;
私人整数计数;
private-Map-wordCount=new-HashMap();
/**
*WordAnalyzer类对象的构造函数
*/
公共词汇分析器(){
wordCount=newhashmap();
WordAnalyzer=newArrayList();
}
/**
*使用for each循环添加输入,从0开始
*/
公共void addWord(字符串输入){
添加(输入);
int counter=wordCount.getOrDefault(输入,0);
wordCount.put(输入,计数器+1);
系统输出打印项次(输入);
}
/**
*获取给定单词被看到的次数。
*
*@param word要查找的单词。
*@return该单词被看到的次数。
*/
public int getCount(字符串字){
整数计数=wordCount.get(word);
如果(计数=null){
返回0;
}
返回计数;
}
}
  • 您可以使用map
    wordCount
    存储单词的重复次数
  • 不需要每次添加新元素时都循环所有元素
  • 当需要对特定单词进行计数时,只需检查
    wordCount
    是否将该单词作为键,或者返回0

每次添加单词时,您都在循环所有单词。你应该删除
addWord
中的for循环,不要破坏你的问题。最好在这里使用一个映射。和我一样。此外:请不要使用像
private ArrayList wordanalyzer这样的名称wordanalyzer
。Pascal大小写用于类名。对于变量名,它使用camelCase。