2013-05-13 112 views
0

我正在計數數組中每個元素的出現,但出現錯誤「Value can not be null」這對我來說沒有意義,因爲arr1完全填充了除最後5元素是null。計算數組中出現的次數

這是我的代碼。我第一次使用字典,所以我可能會在某處出現邏輯錯誤。我正在閱讀文本文件。

string[] arr1 = new string[200]; 
StreamReader sr = new StreamReader("newWorkSheet.txt"); 
string Templine1 = ""; 
int counter = 0; 
while (Templine1 != null) 
{ 
    Templine1 = sr.ReadLine(); 
    arr1[counter] = Templine1; 
    counter += 1; 
} 
sr.Close(); 

// Dictionary, key is number from the list and the associated value is the number of times the key is found 
Dictionary<string, int> occurrences = new Dictionary<string, int>(); 
// Loop test data 
foreach (string value in arr1) 
{ 
    if (occurrences.ContainsKey(value)) // Check if we have found this key before 
    { 
     // Key exists. Add number of occurrences for this key by one 
     occurrences[value]++; 
    } 
    else 
    { 
     // This is a new key so add it. Number 1 indicates that this key has been found one time 
     occurrences.Add(value, 1); 
    } 
} 

// Dump result 
System.IO.StreamWriter sr2 = new System.IO.StreamWriter("OrganizedVersion.txt"); 
foreach (string key in occurrences.Keys) 
{ 
    sr2.WriteLine("Integer " + key.ToString() + " was found " + occurrences[key].ToString() + " times"); 
} 
sr2.Close(); 
Console.ReadLine(); 

編輯:我把所有的代碼放在這裏包括聲明。

+0

顯示'arr1'和'counter'的聲明和初始化。 – 2013-05-13 17:50:35

回答

1

我的錢在arr1爲空(基於事實上,您應該事先知道大小,但您正在填充可能會更改的文件中的行)。好處是你並不需要它。

替換此:foreach (string value in arr1)

...這一點:

foreach(string value in File.ReadLines("fileName")) 
{ 
} 

MSDN File.ReadLines

+0

omg! @Austin Salonen你的解決方案工作!你彈了什麼魔杖? – Harmond 2013-05-13 17:39:57

+0

@Harmond:這只是體驗... – 2013-05-13 17:45:42

+0

這是什麼工作 的foreach(在occurrences.Keys字符串鍵) { Console.WriteLine(key.ToString()+ 「」 +出現[關鍵]的原因.ToString()); } 但如果我做sr2.WriteLine並嘗試將輸出寫入文件,它只會將最後一行寫入文本文件? – Harmond 2013-05-13 17:47:06

4

這確實不是你的問題,但LINQ的在這裏可以減少行數:

var groups = arr1.GroupBy(item => item); 
foreach (var group in groups) 
{ 
    Console.WriteLine(string.Format("{0} occurences of {1}", group.Count(), group.Key); 
} 
+0

是Linq是答案。也許你可以用ToDictionary()添加一個變體。 – 2013-05-13 17:39:34

+1

@HenkHolterman,ToDictionary不會在這裏工作,因爲有多個項目具有相同的密鑰,ToLookup會工作 – 2013-05-13 17:40:25

+1

您可以使用兩者來獲取計數字典,像'.ToLookup(...)。ToDictionary(x = > x.Key,x => x.Count())'。 'GroupBy'可以代替'ToLookup'工作,如果在這個實例中有所不同,那麼不能確定。 – 2013-05-13 17:41:18

0

在你的循環中你需要檢查是否有null在你的價值

foreach (string value in arr1) 
{ 
    if (!string.IsNullOrEmpty(value)) 
    { 
     ........ 

這將帶你可能在文件中的問題護理。

1

都能跟得上 「ARR1完全沒有空值填充」。你放入數組的最後一項是空的。檢查的價值,你把它在數組中前:

while (true) { 
    Templine1 = sr.ReadLine(); 
    if (Templine1 == null) break; 
    arr1[counter++] = Templine1; 
} 

或者,如果你喜歡這個方法好:

while ((Templine1 = sr.ReadLine()) != null) { 
    arr1[counter++] = Templine1; 
} 

現在,環達指數counter,而不是通過整個數組循環不管您放入多少物品:

for (int i = 0; i < counter; i++) { 
    string value = arr1[i]; 
    ... 
} 
+0

你說的最後一項是空的。 – Harmond 2013-05-13 17:48:12

+0

但即使我刪除最後一個空我得到這個錯誤.. – Harmond 2013-05-13 17:56:00

+0

@哈蒙德:你改變了你使用數組的循環? – Guffa 2013-05-13 18:04:05