✅ Index was outside of bounds of the array

I've written a Morse Code translator, and it pretty much works 100% besides the fact it throws an exception "Index was outside of bounds of the array " every time I try to convert from Morse Code to English. The code uses dictionarys and the TryGetValue method to do the conversion.
9 Replies
TypicalSoldier
TypicalSoldier17mo ago
int index = 0; for (int morseIndex = 0; morseIndex <= morseInput.Length; morseIndex++) { string[] letterTokens = morseInput.Split(null); char englishOutput; morseToEnglishDictionary.TryGetValue(letterTokens[index], out englishOutput); morseOutputTextBox.AppendText(englishOutput.ToString()); index++; }
.logik.
.logik.17mo ago
Probably should be morseIndex < morseInput.Length in the for condition. If you have 5 objects your loop will run 6 times for index = 0,1,2,3,4,5. If you change it to < instead of <= then it will only run 5 times. However the way the index variable is redundant because thats what the morseIndex is doing. Also there are operations that you're performing in the loop that only need to be executed once such as morseInput.Split and declaring the char variable. You can look to move them outside the loop to improve the efficiency
TypicalSoldier
TypicalSoldier17mo ago
Thanks, I'll try that. I only added the index outside to try the loop to just try something to get rid of the exception lol I changed it to a while loop and I keep getting the index out of bounds exception char englishOutput; string[] letterTokens = morseInput.Split(null); int morseIndex = 0; while (morseIndex < morseInput.Length) { morseToEnglishDictionary.TryGetValue(letterTokens[morseIndex], out englishOutput); morseOutputTextBox.AppendText(englishOutput.ToString()); morseIndex++; }
.logik.
.logik.17mo ago
Can you share the sample input that you're using?
TypicalSoldier
TypicalSoldier17mo ago
- . ... - is what i'm trying to convert to english, it should be "test"
.logik.
.logik.17mo ago
Oh I've figured it out morseInput.Length is the number of characters in the string but you're splitting based on tokens and iterating over the tokens so you should use letterTokens.Length to limit your loop I still think a for loop is preferred here even better is probably a foreach loop
foreach (string s in letterTokens) {
morseToEnglishDictionary.TryGetValue(s, out englishOutput);
morseOutputTextBox.AppendText(englishOutput.ToString());
}
foreach (string s in letterTokens) {
morseToEnglishDictionary.TryGetValue(s, out englishOutput);
morseOutputTextBox.AppendText(englishOutput.ToString());
}
but hopefully you understand the problem you were having before?
TypicalSoldier
TypicalSoldier17mo ago
thank you so much!!! my gut feeling knew it was something with the .length i was using yes, it was because i was trying to use the length of the string which is very different than the length of the token i owe you 1!!! 🙂
.logik.
.logik.17mo ago
No stress, if theres no other questions, don't forget to $close
MODiX
MODiX17mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server
More Posts
Making a shortcut for checking null component in unityEvetime I create a new component in unity, I something like ``` if (!TryGetComponent(out _rb2D)) {✅ Help with non-nullable field must contain a non-null value when exiting constructor.Following an online tutorial and trying to resolve nullable reference warnings (the tutorial was befC# WPF Login Oracle DatabaseSo I'm making a login system with C# WPF and I use Oracle DB. As I understood oracle it only has one❔ Using mvvm toolkit- Why my view has no access to my command?I got the following Code in the view model: ``` [RelayCommand] public void CalculateAnd❔ Razor Page - Select Tag Helper (form-select): Need help with onchange event for drop-down listIn my APR.cshtml page I have the following drop down list present: `<select asp-for="@Model.ServerNa❔ EF Core setting property with value converter to nulli have a value object ```cs public record ShippingOrderNumber(int Number);``` that i'm mapping to ❔ Some bug i guess**Code that works** ``` Task.Run(() => Static_class_name.START(param1, param2, param3)); ``` *inside❔ Visual Studio not producing templates?Hey everyone, I'm attempting to teach myself C# via this website " https://www.w3schools.com/cs/cs_g❔ Confusing ClientWebSocket.ReceiveAsync() behaviorPicture the following class: ```cs private ClientWebSocket _ws = new(); private Memory<byte> _buffe❔ Better way to do this random password generation programThis program generates a random password then removes duplicates and excludes some chars. I need sug