Hi,Am creating program to translate morse code to text and text to morse code. First it worked fine with basic characeters in dictionary: abcdefghijklmnoprstuvzy0123456789 .Than i want to upgrade it to translate special characters in my language like áéäňöü .Problem is when i write it to my dictionary and give every char key it throws me excpetion : System.ArgumentException: An item with the same key has already been added, even if it is only once in dictionary. Every key value is unique.

This is my reading dictionary code

private void ReadDictionary(string filename)

     {
      string cesta = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
        try {
           string[] lines = File.ReadAllLines(filename,Encoding.UTF8);
        if (lines != null) {

        }
                   foreach (string line in lines)
                {
                    string[] znaky = line.Split('=');
                    string key = znaky[0];
                    string value = znaky[1];
                    this.morzeovka.Add(key, value);
                    this.latinka.Add(value, key);
                }   
            }
          catch (IOException) {
             MessageBox.Show("Nepodarilo sa nacitat slovnik"+ Environment.NewLine + "Skontrolujete umiestnenie slovníka v:  "+cesta,"Chyba",MessageBoxButtons.OK,MessageBoxIcon.Error);
             System.Environment.Exit(1);
         }      
    }
}

This is my dictionary

a=.-
b=-...
c=-.-.
d=-..
e=.
f=..-.
g=--.
h=....
i=..
j=.---
k=-.-
l=.-..
m=--
n=-.
o=---
p=.--.
q=--.-
r=.-.
s=...
t=-
u=..-
v=...-
w=.--
x=-..-
y=-.--
z=--..
1=.----
2=..---
3=...--
4=....-
5=.....
6=-....
7=--...
8=---..
9=----.
0=-----
.=.-.-.-
,=--..--
?=..--..
!=--..-
;=-.-.-.
:=---...
(=--...
)=-.--.-
"=.-..-.
-=-....-
_=..--.-
@=.--.-.
+=.-.-.
/=-..-.
'=.----.
á=.--.-
ä=.-.-
é=..-..
ö=---.
ü=..--
ň=--.--

This is the full exception

System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)

Thanks for any help

PS: When i strat debugging and put breakpoint to line this.latinka.Add(value, key); it throws me exception. So problematic keys-values are

    á=.--.-
    ä=.-.-
    é=..-..
    ö=---.
    ü=..--
    ň=--.--

Edited 1 Year Ago by JOLO528

The problem is that "An item with the same key has already been added."

Every key is not unique in the data that you provided, because you use two dictionaries. I recommend against using "key" and "value" as your variable names, especially when you choose to use the following in one of your dictionaries: this.latinka.Add(value, key) This can cause confusion because a dictionary add is <key, value>.

Solution: Your error occurs in this.latinka.Add(value, key). The variable "value" contains a value that already exists, which happens to be the key for this dictionary. Confused? This is why it is important to choose good variable names. The duplicate is "--..."

Modify your code as follows to see the error:

private void ReadDictionary(string filename)
{
    string key = string.Empty;
    string value = string.Empty;

    string cesta = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
    try
    {
        string[] lines = File.ReadAllLines(filename, Encoding.Unicode);
        if (lines != null)
        {

        }
        foreach (string line in lines)
        {
            string[] znaky = line.Split('=');
            key = znaky[0];
            value = znaky[1];

            try
            {
                this.morzeovka.Add(key, value);
            }//try
            catch (Exception ex)
            {
                string errMsg = "Error (morzeovka): " + ex.Message;
                //errMsg += "(" + ex.StackTrace + ")";
                errMsg += System.Environment.NewLine + System.Environment.NewLine;
                errMsg += "morzeovka key: '" + key + "' morzeovka value: '" + value + "'";

                MessageBox.Show(errMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }//catch

            try
            {
                this.latinka.Add(value, key);
            }//try
            catch (Exception ex)
            {
                string errMsg = "Error (latinka): " + ex.Message;
                //errMsg += "(" + ex.StackTrace + ")";
                errMsg += System.Environment.NewLine + System.Environment.NewLine;
                errMsg += "latinka key: '" + value + "' latinka value: '" + key + "'";

                MessageBox.Show(errMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
    catch (IOException)
    {
        MessageBox.Show("Nepodarilo sa nacitat slovnik" + Environment.NewLine + "Skontrolujete umiestnenie slovníka v:  " + cesta, "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
        System.Environment.Exit(1);
    }

}

I try your code. Only what i change was encoding to UTF8, with unicode it throw exception index out of range in line

value = znaky[1];

With unicode encoding, i find out by debugging that in

key= some long long string with undefined characters like squares

, and value is null. Than throw exception Index out of range

After that changing encoding ,Your code works, but it only show me to windows form what i already know. I know which characters are problematic. I find it by debuging, I need to fix it, why those characters cant be load and program thinks there is already same key with same value in dictionary.

Edited 1 Year Ago by JOLO528

OK, i think now it works only what i have to change was encoding from UTF8 to Default...

Encoding should match the encoding of the file you're using. Using more try-catch blocks (as shown above) will help you to troubleshoot and will cause little to no performance issues since you're using such a small dataset.

Note: '7' and '(' have the same value above. You use the values as the keys for your second dictionary-latinka.

Edited 1 Year Ago by cgeier

This article has been dead for over six months. Start a new discussion instead.