ahmed alshehri

ahmed alshehri

  • NA
  • 1
  • 1.4k

how to count same names in textFile

Jul 26 2012 10:53 AM

I wanna create text file containing one name on each line. Compute the number of times any name occurs. Output one line for each name in file and on each line print the number of occurrences followed by name.


and i would like to diplay the answer in messageBox

this is what i have done so far.


private void button1_Click(object sender, EventArgs e)

{

var nameCount = new Dictionary<string, int>();

foreach (String s in File.ReadAllLines(openFileDialog1.FileName))

{

if (nameCount.ContainsKey(s))

{

nameCount[s] = nameCount[s] + 1;

}

else

{

nameCount.Add(s, 1);

}

}

var output = new StringBuilder();

foreach (var pair in nameCount)

{

MessageBox.Show(String.Format("{0} {1}\n", pair.Key, pair.Value));

}

}

private void button1_Click(object sender, EventArgs e)

{

var nameCount = new Dictionary<string, int>();

foreach (String s in File.ReadAllLines(openFileDialog1.FileName))

{

if (nameCount.ContainsKey(s))

{

nameCount[s] = nameCount[s] + 1;

}

else

{

nameCount.Add(s, 1);

}

}

var output = new StringBuilder();

foreach (var pair in nameCount)

{

MessageBox.Show(String.Format("{0} {1}\n", pair.Key, pair.Value));

}

}




 



 

 


Answers (2)