2014-03-31 61 views
1

我想統計出源字符串中特定單詞的出現次數。 假設src =「thisisamangoterrthisismangorightthis?」 word =「this」 所以我正在做的是,首先搜索src中的單詞索引。它在索引0處。現在我從這個索引位置提取部分到src結尾。 即現在src =「isamangoterrthisismangoright這是什麼?」並再次搜索單詞。 但我越來越數組越界的異常。計算java中單詞出現的次數

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; 

} 
+0

你永遠不會重新計算LEN1,因此它保持了第一個字符串的長度,即使S1變得越來越小,這說明你的異常。只需使用substring(int)從給定索引切換到字符串結尾。 –

回答

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; 
    } 
0

當您使用拋出ArrayIndexOutOfBoundsException的方法,它總是要檢查的範圍是一個好主意。見String#substring

IndexOutOfBoundsException異常 - 如果beginIndex是否定的,或endIndex 比該字符串對象的長度大,或beginIndexendIndex較大 。


您應該涵蓋所有情況:

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

或者,更好的,你應該考慮你的邏輯,以避免在首位這種情況。

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??"); 
} 
0

而不是在你的字符串做substring使用此方法

public int indexOf(int ch, int fromIndex) 

然後就檢查結果是-1

0

您可能會使用替換來解決問題

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

我喜歡這種方式XD,但爲''this'放了一個變量並在count計算中使用它;) –

+0

因爲在這裏替換了文本文本,所以使用'.replace()'而不是'.replaceAll()'。不像它的名字所暗示的那樣,'.replace()'_does_替換了所有的事件。但是,首先,爲什麼在不需要創建新字符串的解決方案存在? ;) – fge

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"));    
} 
} 
相關問題