Click here to Skip to main content
15,898,991 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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

C#
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(openFileDialog1.FileName))
          {
              MessageBox.Show("{0} {1}\n", pair.Key, pair.Value);
          }


but i still have some error
Posted
Comments
[no name] 26-Jul-12 10:25am    
"but i still have some error"... does not tell us anything about whatever problem you are having,
Kaushik Saha from Kolkata,India 26-Jul-12 10:25am    
Where And which error occur
Kaushik Saha from Kolkata,India 26-Jul-12 10:37am    
what you need explain with some content of your files
ahmedalialshehri 26-Jul-12 10:55am    
Could not find file 'C:\Users\acer\AppData\Local\Temporary Projects\FourthProgramm\bin\Debug\openFileDialog1'.

this is the error

1 solution

The problem is in the last part:
C#
foreach (var pair in nameCount(openFileDialog1.FileName))
{
    MessageBox.Show("{0} {1}\n", pair.Key, pair.Value);
}
1) You don't need the (openFileDialog1.FileName) bit.
2) You need to format your string before you can hand it to a messgae box:
C#
foreach (var pair in nameCount)
    {
    MessageBox.Show(String.Format("{0} {1}\n", pair.Key, pair.Value));
    }
But I would do it slightly differently:
C#
foreach (string key in nameCount.Keys)
    {
    MessageBox.Show(string.Format("{0} {1}\n", key, nameCount[key]));
    }
And I really hope you don't have a lot of names, or you will spend all afternoon pressing the "OK" button!



"Could not find file 'C:\Users\acer\AppData\Local\Temporary Projects\FourthProgramm\bin\Debug\openFileDialog1'."

Did you call ShowDialog on the OpenFileDialog at any time?
Normally it would be:
C#
private void button1_Click(object sender, EventArgs e)
    {
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
        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));
            }
        }
    }
 
Share this answer
 
v2
Comments
ahmedalialshehri 26-Jul-12 10:47am    
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));
}
still has some error
and i wanna all the result comes at the same time in MessegeBox
OriginalGriff 26-Jul-12 10:51am    
What is the error message?
And if you want it all in the same box, then replace the message box with output.AppendLine, then display output.ToString() in the message box after the loop.
ahmedalialshehri 26-Jul-12 10:59am    
Could not find file 'C:\Users\acer\AppData\Local\Temporary Projects\FourthProgramm\bin\Debug\openFileDialog1'.
OriginalGriff 26-Jul-12 12:03pm    
Answer updated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900