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));
}
}
Hemant SrivastavaPosted Jul 26, 2012, 11:35 AM
private void button1_Click(object sender, EventArgs e)
{
String sFilePath = "NameFile.txt";
String sLine;
Dictionary
StreamReader strmRdr = new StreamReader(sFilePath);
while ((sLine = strmRdr.ReadLine()) != null)
{
sLine = sLine.Trim();
if (!oNameBag.ContainsKey(sLine))
{
oNameBag.Add(sLine, 1);
}
else
{
oNameBag[sLine] += 1;
}
}
String sOutput = String.Empty;
foreach (KeyValuePair
{
sOutput = sOutput + item.Key + "\t" + item.Value + "\n";
}
MessageBox.Show(sOutput);
}
VulpesPosted Jul 26, 2012, 11:27 AM