计算使用JAVA出现一个单词的次数

时间:2015-11-16 06:08:14

标签: java count stringtokenizer

标记化然后计算单词出现的次数

示例:'快速棕色狐狸快'

预期产出:

< - > - 1 快 - 2 棕色 - 1 狐狸 - 1

[line[n] * line[n+1] for range(0,len(line),2)]

2 个答案:

答案 0 :(得分:1)

您需要在此处使用java.util.Map来维护单词和相应的计数:

import java.util.Map;
import java.util.HashMap;
public class Tokenizer
{
  public static void main(String[] args)
  {
    int index = 0; int tokenCount;
    Map<String,Integer> wordCount = new HashMap<String,Integer>();
    String message="The Quick brown fox the";

    StringTokenizer string = new StringTokenizer(message);

    tokenCount = string.countTokens();
    System.out.println("Number of tokens = " + tokenCount);
    while (string.hasMoreTokens()) { 
          String word = string.nextToken();
          Integer count = wordCount.get(word);
          if(count == null) { //this means the word was encountered the first time
          wordCount.put(word, 1);
        }
        else { //word was already encountered we need to increment the count
          wordCount.put(word, count + 1);
        }
     }
    for (String words : wordCount.keySet())
        { System.out.println("Word : " +  word + " has count :" +wordCount.get(word); 
    }
  }
}

答案 1 :(得分:0)

您可以使用Map

HashMap<String, Integer> wordMap = new HashMap<>(); //here String key will be the word and integer value will be the word count
 while (string.hasMoreTokens()) 
 {

    String word= string.nextToken(); 
    if(wordCount.get(word) == null){ //if string is not present in map, add this occurence with wordCount as 1
        wordMap.put(word,1);
    }
    else{   //if string is already present in map, increment the wordCount by 1
        int count = wordCount.get(word);
        wordwordCount.put(word, count+1);
    }
}
//now traverse the hashmap and print the word along with its count

希望这有帮助!

祝你好运!