Question by
lilchiker · Nov 28, 2020 at 12:23 PM ·
dictionary
I've been trying to have a dictionary look up for a word entered by the player. The words.ContainKey(word) is always returning false, even when it should return true.
public void Awake()
{
WordDictionary dictionary = JsonUtility.FromJson<WordDictionary>(JsonFileReader.LoadJsonAsResource("EnglishDictionary.json"));
Word[] wordList = dictionary.words;
foreach (Word w in wordList)
{
//add all of the words.
if (words.ContainsKey(w.word)){
words.Add(w.word, w);
}
}
}
public void TestWord(string word)
{
if(HasWord(word))
{
Word w = GetWord(word);
Debug.Log(w.pos);
Debug.Log(w.word);
Debug.Log(w.definitions[0]);
}
else
{
Debug.Log("Word not found.");
}
}
public bool HasWord(string word)
{
if(words.ContainsKey(word))
{
return true;
}
return false;
}
public Word GetWord(string word)
{
return words[word];
}
Comment
Answer by lilchiker · Nov 29, 2020 at 04:37 AM
@duck @Bunny83 @Eric5h5 @robertbu @aldonaletto @tanoshimi @whydoidoit @fafase @Statement @clunk47, I have been to build a spelling game, where words spelled must be contained in the English dictionary, 'part of speech' entered for the word must also match the spelled word. My problem is, when the word is entered for a look up in the dictionary keys, the bool is always returning false. Can anyone please walk me through how the word is sent to the HasWord function? or any other directive Thank you