2012-01-05 164 views
0

在這裏,我想讀其中只包含integers.in每line.For例如文本文件:閱讀文本文件

1 
2 

3 
1 

我寫了下面的代碼讀取文本文件。代碼如下所示。

package fileread; 
import java.io.*; 

public class Main { 


public static void main(String[] args) { 
    // TODO code application logic here 
    try{ 
     FileInputStream fstream=new FileInputStream("C:/Users/kiran/Desktop/text.txt"); 
     DataInputStream in=new DataInputStream (fstream); 
     BufferedReader br=new BufferedReader(new InputStreamReader(in)); 
     String str; 
     while((str=br.readLine())!=null){ 
      System.out.println(str); 
     } 
     in.close(); 
    } 
    catch(Exception e){ 
     System.err.println(e); 
    } 
} 

} 

現在我只想檢索那些重複顯示給用戶的整數。 在這種情況下,我想顯示「1」。

我該如何在Java中實現這個?

+0

想要從文件中讀取所有1個文件並顯示給用戶嗎? – 2012-01-05 14:34:44

+0

如果這是家庭作業,它應該被標記爲這樣。 – Perception 2012-01-05 14:48:33

回答

1
package fileread; 
import java.io.*; 
import java.util.HashSet; 
import java.util.Set; 

public class Main { 


public static void main(String[] args) { 
    Set<String> uniqueLines = new HashSet<String>(); 
    Set<String> duplicatedLines = new HashSet<String>(); 
    try{ 
     FileInputStream fstream=new FileInputStream("C:/Users/kiran/Desktop/text.txt"); 
     DataInputStream in=new DataInputStream (fstream); 
     BufferedReader br=new BufferedReader(new InputStreamReader(in)); 
     String str; 
     while((str=br.readLine())!=null){ 
      if (uniqueLines.contains(str)) { 
       if (!duplicatedLines.contains(str)) { 
        duplicatedLines.add(str); 
        System.out.println(str); 
       } 
      } else { 
       uniqueLines.add(str); 
      } 
     } 
     in.close(); 
    } 
    catch(Exception e){ 
     System.err.println(e); 
    } 
} 

} 

注意:確保您的輸入在每行上沒有尾隨空白。另外請注意,當列表變長時,這個實現不是特別的內存友好。

+0

這將打印重複'N'次的'N-1'項副本。 – dasblinkenlight 2012-01-05 14:36:43

+0

@dasblinkenlight:好點。我修好了它。 – Asaph 2012-01-05 14:44:23

1

您需要讀取數組中的值,然後在該數組中找到重複的條目。

0

完全讀取文件,將行保存到您選擇的數據結構(map(key = line,value = count),array(如果只有整數)),枚舉數據結構並打印其值大於1 (如果該值表示計數)。

或即時:讀取文件,將條目添加到set/list/array(如果不包含在set/list/array中),否則打印出行。

0

那麼,你可以使用一個有10個插槽的陣列,它映射到0到9之間的一個數字。對於每一行,你檢查這個數字是什麼,並相應地增加數組中的值。這將是這樣的:

// Initialize the array 
int[] numberArray = new int[10]; 
for (int i = 0 ; i < 10 ; i++) numberArray[i] = 0; 

while((str=br.readLine())!=null){ 
    int number = Integer.parseInt(str); 
    numberArray[number]++; 
} 

for (int i = 0 ; i < 10 ; i++) {\ 
    if (numberArray[i] > 1) System.out.println(i); 
} 
+0

:P好了,突然間我意識到你可能有比9大的數字。如果是這樣,我的方法將無法工作^^ – 2012-01-05 14:38:50

0

除了給出答案,請確保您正在轉換你的字符串到整數(數字),趕上的情況下,除了任何來自該文件不是一個數字。在這種情況下,我認爲您可以安全地忽略異常,因爲它不相關,但檢查輸入數據是一種很好的做法。

0

像這樣的事情

package fileread; 

import java.io.*; 

import java.util.*; 

public class Main { 

public static void main(String[] args) { 

    Hashtable ht = new Hashtable(); 

    try{ 
     FileInputStream fstream = 
      new FileInputStream("C:/Users/kiran/Desktop/text.txt"); 

     DataInputStream in=new DataInputStream (fstream); 

     BufferedReader br=new BufferedReader(new InputStreamReader(in)); 

     String str; 

     while((str=br.readLine())!=null){ 

      String sproof = (String) ht.get(str.trim()); 
      if (sproof != null && sproof.equals("1")) { 
       System.out.println(str); 
      } else { 
       ht.put(str.trim(), "1"); 
      } 
     } 
     in.close(); 
    } 
    catch(Exception e){ 
     System.err.println(e); 
    } 
} 

} 
0

首先,我會定義1和表1個整數集,如下圖所示:

ArrayList<Integer> intList = new ArrayList<Integer>(); 
Set<Integer> duplicateIntSet = new HashSet<Integer>(); //Set is used to avoid duplicates 

然後,我會重複檢查,並添加「時間如下:

while((str=br.readLine())!=null){ 
    if(!str.isEmpty()) { 
     Integer i = Integer.parseInt(str); 

     if(intList.contains(i)) { 
      duplicateIntSet.add(i); 
     } else { 
      intList.add(i); 
     } 
    } 
} 
+0

'intList'的內容是什麼 – RanRag 2012-01-05 14:46:01

0

我會用兩套方法;

public static void main(String[] args) { 
    Set<Integer> result = new HashSet<Integer>(); 
    Set<Integer> temp = new HashSet<Integer>(); 

    try{ 
     FileInputStream fstream=new FileInputStream("text.txt"); 
     DataInputStream in=new DataInputStream (fstream); 
     BufferedReader br=new BufferedReader(new InputStreamReader(in)); 
     String str; 
     while((str=br.readLine())!=null){ 
      if (!"".equals(str.trim())){ 
       try { 
        Integer strInt = new Integer(str.trim()); 
        if(temp.contains(strInt)){ 
         result.add(strInt); 
        } else { 
         temp.add(strInt); 
        } 
       } catch (Exception e){ 
        // usually NumberFormatException 
        System.err.println(e); 
       } 
      } 
     } 
     in.close(); 
    } 
    catch(Exception e){ 
     System.err.println(e); 
    } 
    for(Integer resultVal : result){ 
     System.out.println(resultVal); 
    } 
} 

或者,你也可以使用一個單一的HashMap與HashMap.Key作爲整數和HashMap.Value作爲計數爲整數。 然後,如果您以後需要重構查找具有單個事件的所有實例,那麼您可以輕鬆地完成此操作。

public static void main(String[] args) { 
    Map<Integer, Integer> frequency = new HashMap<Integer, Integer>(); 

    try{ 
     FileInputStream fstream=new FileInputStream("text.txt"); 
     DataInputStream in=new DataInputStream (fstream); 
     BufferedReader br=new BufferedReader(new InputStreamReader(in)); 
     String str; 
     while((str=br.readLine())!=null){ 
      if (!"".equals(str.trim())){ 
       try { 
        Integer strInt = new Integer(str.trim()); 
        int val = 1; 
        if(frequency.containsKey(strInt)){ 
         val = frequency.get(strInt).intValue() + 1; 
        } 
        frequency.put(strInt, val); 
       } catch (Exception e){ 
        // usually NumberFormatException 
        System.err.println(e); 
       } 
      } 
     } 
     in.close(); 
    } 
    catch(Exception e){ 
     System.err.println(e); 
    } 
    // this is your method for more than 1 
    for(Integer key : frequency.keySet()){ 
     if (frequency.get(key).intValue() > 1){ 
      System.out.println(key); 
     } 
    } 
    // This shows the frequency of values in the file. 
    for(Integer key : frequency.keySet()){ 
     System.out.println(String.format("Value: %s, Freq: %s", key, frequency.get(key))); 
    } 
} 

小心NumberFormatExceptions,並根據您的情況,您可以處理它們的循環中,或外循環。