2014-10-29 126 views
0

這是我的程序,用於包含大量數據(如約3315個字)的文件中的字數統計。我不想使用哈希映射。我已經使用哈希映射並獲得輸出,但我的任務是找到另一種方法來查找兩個程序之間的頻率和執行時間差異(即使用哈希映射和程序的程序沒有一個)。我必須使用哈希映射或集以外的任何東西。無輸出...無法理解錯誤

請幫忙..

在此先感謝。

package thirdassignments; 

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Scanner; 
import java.util.SortedSet; 
import java.util.TreeSet; 

public class WordFreq2 { 

public void Working() 
{ 

    FileInputStream in = null; 
    try { 
     in = new FileInputStream("C:/Users/kishansr/Desktop/file1.txt"); 
    } 
    catch (FileNotFoundException ex) { 
     System.err.println("can’t open "); 
     System.exit(1); 
    } 

    String word[]=new String[100000]; 
    int count[]={0},count1=0; 
    Scanner input = new Scanner(in); 

    //map<String,Integer> freq = new HashMap<String,Integer>(); 
    while (input.hasNext()) { 
     count1=count1+1; 
    } 
    System.out.println(" Count: "+ count1); 
    for(int i=0;i<=count1;i++) 
    { 
     String word1 = input.next().toLowerCase(); 
     System.out.println("word1 : " +word); 
     if(word[i] != word1) 
     { 
      word[i]=word1; 
      count[i]=1; 
     } 
     else if(word[i]==word1) 
     { 
      count[i]=count[i]+1; 
     } 

    } 

     for (int i=0;i<count1;i++) { 
     System.out.println(count[i] + " : " + word[i]); 
    } 


} 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    WordFreq2 wf = new WordFreq2(); 
    long startruntime = System.nanoTime(); 
    wf.Working(); 
    long endruntime = System.nanoTime(); 
    System.out.println("start time: "+startruntime+" ,end time :"+endruntime+" ,difference time: "+(endruntime - startruntime)+" nano seconds "); 
} 

} 
+2

您可能會忘記在while(input.hasNext()){}內調用'input.next()',這就是爲什麼它被轉換爲**無限**循環的原因。始終在同一個循環中使用這兩種方法。 – Braj 2014-10-29 11:39:44

+0

謝謝...明白了.. – 2014-10-29 15:44:42

回答

0

使用正則表達式匹配的圖案,獲得第一個字,詞匹配剩餘串並獲得它的計數,打印計數與空白和重複過程仍然是所有的話拿完更換字呢。

+0

謝謝... willl檢查出來.... – 2014-10-29 12:09:56

+0

ans很有用嗎? – 2014-10-30 08:40:45

+0

這又需要使用哈希映射..我不能使用哈希映射或樹木.. – 2014-10-30 12:56:02