文本文件中有多少个唯一单词以及这些唯一单词的计数是多少

时间:2015-11-25 17:50:39

标签: java arrays

import java.io.FileReader;
import java.util.Arrays;


public class dictionary {

    public static void main(String[] args) throws IOException {

        // Read all the words from the dictionary (text.txt) into an array
        BufferedReader br = new BufferedReader(new FileReader("text.txt"));
        int bufferLength = 1000000;
        char[] buffer = new char[bufferLength];
        int charsRead = br.read(buffer, 0, bufferLength);
        br.close();
        String text = new String(buffer);
        text = text.trim();
        text = text.toLowerCase();
        String[] words = text.split("\n");

        System.out.println("Total number of words in text: " + words.length);

        //to find for unique text
        String[] uniqueText = {};
        int[] textCount = new int[uniqueText.length];

        for (int i = 0; i < words.length; i++) {

            if (uniqueText[i].contains(words[i])) {
                textCount[i]++;

            } else {
                uniqueText[i] = words[i];

            }

        }
    }

嗨,我在这里找不到我的代码而且我真的不知道如何用文字表达我的问题,所以我会使用伪代码。

线程中的异常&#34; main&#34;

实现了java.lang.NullPointerException

if(uniqueText [i] .contains(words [i]))

  

创建一个字符串数组[uniqueText]
  创建一个int数组[uniqueTextCount]

   For every word in text

      if word[i] exists in uniqueText(
            +uniqueTextCount[i])
      else(
           + the new unique word to uniqueText)

1 个答案:

答案 0 :(得分:1)

此处的问题是您正在初始化空白数组String[] uniqueText = {};,然后尝试添加到该数组(uniqueText[i] = words[i];)。我相信你正在寻找的东西就像List,其大小可以是动态的,因为我们从一开始就不知道有多少独特的单词。或者我们甚至可以向前迈出一步并使用Map,它也具有动态大小,但使用键和值,因此我们希望将单词与计数相关联,因此非常适合这种情况。

System.out.println("Total number of words in text: " + words.length);

Map<String, Integer> uniqueWordsAndCount = new HashMap<>();
for (String word : words) { 
    if (uniqueWordsAndCount.containsKey(word)){ //If word is in our map already, increase count
        uniqueWordsAndCount.put(word, uniqueWordsAndCount.get(word)+1);
    }else{  //If not in our map, add it and set count to 1
        uniqueWordsAndCount.put(word, 1);
    }
} 
//Accessing the count of a word
uniqueWordsAndCount.get("someWord"); //This returns the count