Now, i already split the data. I want to read the data inside the foreach loop (sortedlist) but it always give me error.

while ((read = f.ReadLine()) != null)
                        {
                            s1.Append(read);
                        
                            //read = read.Replace("\r","");

                            SplitItem = (read).Split(new string []{"\r\n",":"},StringSplitOptions.RemoveEmptyEntries);

                            foreach (String abc in SplitItem)
                            {
                                a += "\r" + abc;
                            }
                                               
                     
                         // MessageBox.Show(s1.ToString().Trim());
                         // showFile1.Items.Add(s1.ToString().Trim());
                       
                        }
                       // MessageBox.Show(a.ToString().Trim());
                      

                        foreach (string item in SplitItem)
                        {
                            if (!(listItem.ContainsKey(item)))
                            {
                                listItem.Add(item, 1);
                            }
                            else
                            {
                                int count = (int)listItem[item];
                                listItem[item] = count + 1;
                            }
                        }

hope someone can help me..thanks ya

hey..Now i got one problem need someone to help me. Now, i do the split and sortedlist and print my items.But, my output is contain some items that i do not want.Hope somebody can help me.
The output that i need:
{1 2}:2
{1 3}:3
{1 4}:3
{2 3}:1
{2 4}:2
{3 4}:3
Now, My program give me this:
{1}:10
{1 2}:2
{1 3}:3
{1 4}:3
{2}:4
{2 3}:1
{2 4}:2
{3 4}:3
the single item({1}:10 and {2}:4) is not what i want.So, what should i do to solve it..?

while ((read = f.ReadLine()) != null)
                        {
                            // s1.Append(read);
                        
                            //read = read.Replace("\r","");

                            SplitItem = (read).Split(new string []{"\r\n",":"},StringSplitOptions.RemoveEmptyEntries);

                           /* foreach (String abc in SplitItem)
                            {
                               a += "\r\n" + abc;
                            }*/
                     
                         // MessageBox.Show(s1.ToString().Trim());
                         // showFile1.Items.Add(s1.ToString().Trim());  
                         // MessageBox.Show(a.ToString().Trim());
                      
                        foreach (string item in SplitItem)
                        {
                            if (!(listItem.ContainsKey(item)))
                            {
                                listItem.Add(item, 1);
                            }
                            else
                            {
                                int count = (int)listItem[item];
                                listItem[item] = count + 1;
                            }
                        }
                     }     
                        IDictionaryEnumerator enumerator = listItem.GetEnumerator();
                        showFile1.Items.Clear();
                        while (enumerator.MoveNext())
                        {
                            showFile1.Items.Add( "{"+enumerator.Key.ToString() +"}:"+ enumerator.Value.ToString() + "\r\n");

                        }

Man, how do I know what is "SplitItem" variable? Is this a string (which cannot be), is an array or any other collection type?
PLEASE, try to paste all the code needed for us, if you want any decent help.
Lets says its an array (string[]). The same goes for "listItem. Is this a generic list( Dictionary<string, int> or is a SortedList<string, int> ?

You see, I am guessing, even if I am close to what it might be.
Lets see now what you can do:

string[] items = { "A", "B", "A", "C", "B", "A" };
            Dictionary<string, int> dic = new Dictionary<string,int>();

            foreach (string item in items)
            {
                if (!dic.ContainsKey(item))
                    dic.Add(item, 1);
                else
                {
                    int a = dic[item];
                    dic[item] = a + 1;
                }
            }

Hope it helps,
Mitja

The "SplitItem" is a String Array...

Same as in mine example (I have only renamed it to "items".

But,the result still the same. Now, i know my problem is at where but i dun know how to solve it.
My item that i have:
(Key:Value)
1 2:1
1 3:2
1 4:2
2 3:1
2 4:1
3 4:2
1 3:1
1 4:1
3 4:1
1 2:1
1 3:1
1 4:2
2 4:1
3 4:1
After i read the file by line, i need to split those items. After that, i need to do the sortedlist to add up the items.Now, the problem is when i split

SplitItem = (read).Split(new string []{"\r\n",":"},StringSplitOptions.RemoveEmptyEntries);

My item will become like this:
1 2
1
1 3
2
1 4
2
2 3
1
2 4
1
3 4
2
1 3
1
1 4
1
3 4
1
1 2
1
1 3
1
1 4
2
2 4
1
3 4
1
After that, i need to add up the those item like this:
{Key: value}
{1}:10
{1 2}:2
{1 3}:3
{1 4}:3
{2}:4
{2 3}:1
{2 4}:2
{3 4}:3
But, Now my output have the {1}:10 and {2}:4 inside my output.These two items which i don't want. They appear because when i splitting the items and adding up, the program will add up how many value"1" appear and how many value "2" appear in the output.
I know what happen to my program but i don't know how to solve it.

private void CLICK1_Click(object sender, EventArgs e)
        {
             using (OpenFileDialog fileOpen = new OpenFileDialog())
            {
                String path = @"C:\Users\Fish\Desktop\fyp-coding\Combine1.txt";

                if (File.Exists(path))
                {
                    StreamReader f = null;

                    try
                    {
                        f = new StreamReader(path);

                        while ((read = f.ReadLine()) != null)
                        {
                            // s1.Append(read);
                        
                            read = read.Replace("\r","");
                             //SplitItem is string Array
                            SplitItem = (read).Split(':');

                          foreach (String abc in SplitItem)
                            {
                               a +="\r\n"+ abc; 
                            }
                           // MessageBox.Show(a.ToString().Trim());
                           // MessageBox.Show(s1.ToString().Trim());
                           // showFile1.Items.Add(s1.ToString().Trim()); 

                        foreach (string item in SplitItem)
                        {
                           // MessageBox.Show(SplitItem.ToString());
                            if (!(listItem.ContainsKey(item)))
                            {
                                listItem.Add(item, 1);
                            }
                            else
                            {
                                int count = (int)listItem[item];
                                listItem[item] = count + 1;
                            }
                        }  
                        }
                         
                        IDictionaryEnumerator enumerator = listItem.GetEnumerator();
                        showFile1.Items.Clear();
                        while (enumerator.MoveNext())
                        {
                            showFile1.Items.Add( "{"+enumerator.Key.ToString() +"}:"+ enumerator.Value.ToString() + "\r\n");

                        }
                     // MessageBox.Show(result.ToString().Trim());                  
                       
                    }
                    finally
                    {
                        if (f != null)
                            f.Close();
                    }
                }

            }
        }

Hope someone can help me..thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.