Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Collections Sets and Dictionaries Dictionary Keys and Values

Error in translation

I've written the following code to translate to text

public static string ToText(string input) { List<string> text = new List<string>(input.Length); foreach (char c in input) { try { char morseText = _MorseToText[c.ToString()]; text.Add(morseText.ToString()); } catch (KeyNotFoundException) { text.Add("!"); } } return string.Join(" ", text); }

The problem is its translating wrong. If i input -.-.-- it should produce !, but it produces T E T E TT

1 Answer

Steven Parker
Steven Parker
231,269 Points

Since the loop in this code examines every character individually, every element is being translated to either an E or a T. To get a correct translation, the lookup will need to be performed on complete groups of elements. Try processing the input that way instead.

An example of how this can be done is provided in the "final_code" section of the Project Files download