计算java中单词的出现次数

时间:2014-03-31 07:35:49

标签: java string pattern-matching

我想计算源字符串中特定单词的出现次数。 让我们说src =" thisisamangoterrthisismangorighttighttohist?"      字="这" 所以我要做的是,首先在src中搜索单词索引。它在索引0处。现在我将该部分从此索引位置提取到src的末尾。 即,现在src =" isamangoterrthisismangorightthis?"并再次搜索单词。 但是我得到的数组超出了绑定范围。

public static int countOccur(String s1, String s2)
{
    int ans=0;
    int len1=s1.length();
    int len2=s2.length();
    System.out.println("Lengths:"+len1+" " +len2);

    while(s1.contains(s2))
    {
        ans++;
        int tmpInd=s1.indexOf(s2);
        System.out.println("Now Index is:"+tmpInd);
        if((tmpInd+len2)<len1){
            s1=s1.substring(tmpInd+len2, len1);
            System.out.println("Now s1 is:"+s1);
        }
        else
            break;
    }
    return ans;

}

6 个答案:

答案 0 :(得分:0)

当您使用抛出ArrayIndexOutOfBoundsException的方法时,检查边界始终是个好主意。见String#substring

  

IndexOutOfBoundsException - 如果beginIndex为否定,或endIndex   大于此String对象的长度,或beginIndex   大于endIndex


你应该涵盖所有情况:

if(tmpInd + len2 >= s1.length() || len1 >= s1.length() || ... ) {
    //Not good
}

或者,更好的是,你应该首先考虑你的逻辑来避免这种情况。

答案 1 :(得分:0)

尝试并使用indexOf(),它会照顾你的界限等:

public static int countOccurrences(final String haystack, final String needle)
{
    int index = 0;
    int ret = 0;
    while (true) {
        index = haystack.indexOf(needle, index);
        if (index == -1)
            return ret;
        ret++;
    }

    // Not reached
    throw new IllegalStateException("How on earth did I get there??");
}

答案 2 :(得分:0)

不要在String上使用substring而是使用此方法

public int indexOf(int ch, int fromIndex)

然后检查结果是否为-1

答案 3 :(得分:0)

您可能会使用替换来解决问题

String s = "thisisamangoterrthisismangorightthis?";
String newS = s.replaceAll("this","");
int count = (s.length() - newS.length()) / 4;

答案 4 :(得分:0)

import java.io.*;
import java.util.*;

public class WordCount
{
public static class Word implements Comparable<Word>
{
    String word;
    int count;

    @Override
    public int hashCode()
    {
        return word.hashCode();
    }

    @Override
    public boolean equals(Object obj)
    {
        return word.equals(((Word)obj).word);
    }

    @Override
    public int compareTo(Word b)
    {
        return b.count - count;
    }
}


    public  static void findWordcounts(File input)throws Exception
    {
       long time = System.currentTimeMillis();

    Map<String, Word> countMap = new HashMap<String, Word>();

    BufferedReader reader = new BufferedReader(new InputStreamReader(new    FileInputStream(input)));
    String line;
    while ((line = reader.readLine()) != null) {
        String[] words = line.split("[^A-ZÅÄÖa-zåäö]+");
        for (String word : words) {
            if ("".equals(word)) {
                continue;
            }

            Word wordObj = countMap.get(word);
            if (wordObj == null) {
                wordObj = new Word();
                wordObj.word = word;
                wordObj.count = 0;
                countMap.put(word, wordObj);
            }

            wordObj.count++;
        }
    }

    reader.close();

    SortedSet<Word> sortedWords = new TreeSet<Word>(countMap.values());
    int i = 0;
    for (Word word : sortedWords) {
        if (i > 10) {
            break;
        }

        System.out.println("Word \t "+ word.word+"\t Count \t"+word.count);

        i++;
    }

    time = System.currentTimeMillis() - time;

    System.out.println("Completed in " + time + " ms"); 
    }


public static void main(String[] args)throws Exception
{
   findWordcounts(new File("./don.txt"));               
}
}

答案 5 :(得分:0)

尝试这个来计算字符串中的单词,

private static int countingWord(String value, String findWord)
    {
        int counter = 0;
        while (value.contains(findWord))
        {
            int index = value.indexOf(findWord);
            value = value.substring(index + findWord.length(), value.length());
            counter++;
        }
        return counter;
    }