2
Answers

how to count same names in textFile

ahmed alshehri

ahmed alshehri

12y
1.4k
1

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)
Next Recommended Forum