Search code examples
c#idictionary

IDictionary Value overwrite


This is my first time when I need to do something with dictionaries. I can't overwrite the item.Value and i don't know how should I do that.

Write a program that reads the name of one monkey per line from the lines of standard input to the end of file (EOF) and the number of bananas it collects in the following format:

monkey_name; number of bananas

The program writes the names of the monkeys and the number of bananas they have collected to the standard output in the form given in the example output, in lexicographically ascending order based on the names of the monkeys!

Input:

Jambo;10
Kongo;5
Charlie;12
Jambo;10
Koko;14
Kongo;10
Jambo;5
Charlie;8 

Output:

Charlie: 20
Jambo: 25
Koko: 14
Kongo: 15

Here is my code:

string input;
string[] row = null;
IDictionary<string, int> majom = new SortedDictionary<string, int>();
int i;
bool duplicate = false;
while ((input = Console.ReadLine()) != null && input != "")
{
    duplicate = false;
    row = input.Split(';');
    foreach(var item in majom)
    {
        if(item.Key == row[0])
        {
            duplicate = true;
            i = int.Parse(row[1]);
            item.Value += i; //I'm stuck at here. I dont know how am i able to modify the Value
        }
    }
    if(!duplicate)
    {
        i = int.Parse(row[1]);
        majom.Add(row[0], i);
    }                
}
foreach(var item in majom)
{
    Console.WriteLine(item.Key + ": " + item.Value);
}

Sorry for my bad English I tried my best.


Solution

  • class Program
    {
        static void Main()
        {
            string input;
            string[] row;
            IDictionary<string, int> majom = new SortedDictionary<string, int>();
    
            //int i;
            //bool duplicate = false;
    
            while ((input = Console.ReadLine()) != null && input != "")
            {
                //duplicate = false;
                row = input.Split(';');
    
                // Usually dictionaries have key and value
                // hier are they extracted from the input
                string key = row[0];
                int value = int.Parse(row[1]);
    
                // if the key dose not exists as next will be created/addded
                if (!majom.ContainsKey(key))
                {
                    majom[key] = 0;
                }
    
                // the value coresponding to the already existing key will be increased
                majom[key] += value;
    
                //foreach (var item in majom)
                //{
                //    if (item.Key == row[0])
                //    {
                //        duplicate = true;
                //        i = int.Parse(row[1]);
                //        item.Value += i; I'm stuck at here. I dont know how am i able to modify the Value
                //    }
                //}
                //if (!duplicate)
                //{
                //    i = int.Parse(row[1]);
                //    majom.Add(row[0], i);
                //}
            }
    
            foreach (var item in majom)
            {
                Console.WriteLine(item.Key + ": " + item.Value);
            }
        }  
    }