2015-10-07 14 views
0

我開發了一個java代碼,它將文本文件作爲輸入並選擇重複單詞並通過創建包含重複單詞的新文本文件給出輸出,現在我需要它來選擇三重複的話,但我不能正確地得到它。下面是我的Java代碼 -錯誤的價值,同時獲得三重副本

import java.util.*; 
import java.io.*; 
public class CheckDuplicate { 


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

    // TODO Auto-generated method stub 

    FileReader file1=new FileReader("/home/goutam/workspace/DuplicateWord/clean_2014.txt"); 

    BufferedReader reader1=new BufferedReader(file1); 

    File f=new File("Reduplication.txt"); 

    FileWriter fw=new FileWriter(f); 

    String line=reader1.readLine(); 

    while(line!=null){ 

     String[] arr=line.split(" "); 

     if(arr.length>1){ 

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

       if(i<arr.length-1){ 

        int cmp=arr[i].compareTo(arr[i+1]); 

        if(cmp==0){ 

         fw.write(arr[i].toString()); 

         fw.write("\n"); 

        } 

       } 

      } 
     } 

     line=reader1.readLine(); 

    } 
    reader1.close(); 

    file1.close(); 
} 

}

+0

什麼結果你得到? – proudandhonour

+0

發佈輸出並告訴輸出有什麼問題,這種方式很容易調試 –

+0

當你說「triple duplicated」時,你的意思是連續出現三次?您的程序只檢查連續出現兩次的單詞。 – panonski

回答

4

您的代碼不工作,因爲你只考慮相鄰元素。

您可以使用Map來代替嵌套循環,使用Map表示字符串作爲值並指定計數值作爲整數。

  • 當你第一次遇到一個字符串,你的1
  • 值插入它時,你有一個字符串中已有的地圖,只需增加它的價值

然後你可以遍歷在值上,並選擇值>你想要的鍵。

我強烈建議您使用調試器,它可以幫助您更好地理解程序的流程。

0

既然你想要的物品出現3次在一排,我修改了代碼來實現自己的目標:

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

    FileReader file1 = new FileReader("/home/goutam/workspace/DuplicateWord/clean_2014.txt"); 

    BufferedReader reader1 = new BufferedReader(file1); 

    File f = new File("Reduplication.txt"); 

    FileWriter fw = new FileWriter(f); 

    String line = reader1.readLine(); 

    while (line != null) { 

     String[] arr = line.split(" "); 

     if (arr.length > 1) { 

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

       if (i < arr.length - 2) { // change from length-1 to length-2 

        int cmp = arr[i].compareTo(arr[i + 1]); 

        if (cmp == 0) { 
         if (arr[i + 1].equals(arr[i + 2])) { // keep comparing the next 2 items 

          System.out.println(arr[i].toString() + "\n"); 

          fw.write(arr[i].toString()); 
          fw.write("\n"); 
         } 
        } 
       } 
      } 
     } 

     line = reader1.readLine(); 

    } 
    reader1.close(); 

    file1.close(); 
} 
0

這應該做的工作,注意:我沒有編譯,也不測試,但在至少它應該爲您提供一些指導。

public void findRepeatingWords(int atLeastNRepetitions) { 
    try (BufferedReader reader1 = new BufferedReader(new FileReader("/home/goutam/workspace/DuplicateWord/clean_2014.txt"))) { 
     // There are libraries that can do this, but yeah... doing it old style here 
     // Note that usage of AtomicInteger is just a convenience so that we can reduce some lines of codes, not used for atomic operations 
     Map<String, AtomicInteger> m = new LinkedHashMap<String, AtomicInteger>() { 
      @Override 
      public AtomicInteger get(Object key) { 
       AtomicInteger cnt = super.get(key); 
       if (cnt == null) { 
        cnt = new AtomicInteger(0); 
        super.put(key, cnt); 
       } 
       return cnt; 
      } 
     }; 

     String line = reader1.readLine(); 
     while(line!=null){ 
      // Note we use \\W here that means non-word character (e.g. spaces, tabs, punctuation,...) 
      String[] arr = line.split("\\W"); 
      for (String word : arr) { 
       m.get(word).incrementAndGet(); 
      } 
      line = reader1.readLine(); 
     } 
    } 
}  

private void writeRepeatedWords(int atLeastNRepetitions, Map<String, AtomicInteger> m) { 
    File f = new File("Reduplication.txt"); 
    try (PrintWriter pw = new PrintWriter(new FileWriter(f))) { 
     for (Map.Entry<String, AtomicInteger> entry : m.entrySet()) { 
      if (entry.getValue().get() >= atLeastNRepetitions) { 
       pw.println(entry.getKey()); 
      } 
     } 
    } 
} 
0

這裏是你正在尋找的東西,我一直在使用LinkedHashMap的執行它,這是一個動態的代碼,你選擇不僅雙人,三人也去的時間n個。

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.util.LinkedHashMap; 
import java.util.Map; 
import java.util.Map.Entry; 

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

    BufferedReader reader1 = new BufferedReader(new java.io.FileReader(
      "src/Source/A3_data")); 

    PrintWriter duplicatewriter = new PrintWriter(
      "src/Source/A3_out_double", "UTF-8"); 
    PrintWriter tripleduplicatewriter = new PrintWriter(
      "src/Source/A3_out_tripple", "UTF-8"); 

    LinkedHashMap<String, Integer> map = new LinkedHashMap<>(); 
    String line = reader1.readLine(); 
    while (line != null) { 

     String[] words = line.split(" "); 
     int count = 0; 

     while (count < words.length) { 
      String key = words[count]; 
      Integer value = map.getOrDefault(key, 0) + 1; 

      map.put(key, value); 
      count++; 
     } 
     line = reader1.readLine(); 
    } 

    for (Entry<String, Integer> entry : map.entrySet()) { 
     if (entry.getValue() == 2) 
      duplicatewriter.println(entry.getKey()); 

     if (entry.getValue() == 3) 
      tripleduplicatewriter.println(entry.getKey()); 

    } 
    duplicatewriter.close(); 
    tripleduplicatewriter.close(); 
} 
} 
0

試一試這段代碼打印,如果計數大於3,你可以使用任意數量

public static void getStringTripple(String a){  
     String s[]=a.split(" "); 
     List<String> asList = Arrays.asList(s); 
     Set<String> mySet = new HashSet<String>(asList); 
     for(String ss: mySet){ 
      if(Collections.frequency(asList,ss)>=3) 
       System.out.println(ss + " " +Collections.frequency(asList,ss)); 
     }  
    }