+ 3
How do I search for a String in a dictionary and return the associated value? (Language Translator)
In the class DictionSeq that extends DictionBase, I have a method called translateS. It should search for a string in the dictionary and if found, the associated data value is returned; otherwise the String “No comprehendo” should be returned. I started but I don’t know how to perform the search. I would really appreciate the help The value returned from getkey is the English word at that position and the value returned from getVal is the Spanish word https://code.sololearn.com/cVbi5yUiOTAK/?ref=app
19 Answers
+ 3
What you need to know is where do you find the english word? In which array index? You need to scan the dictionary array for English words, for that you need to know what the dictionary looks like as a runtime structure. Is it dictArr[n][0] for any non-negative n?
+ 3
Okay, then all you need to do, I suppose, is to walk through the length (method getCount()) of the array, for each key returned from getKey, compare if it matches the english word you want to translate (method parameter), and if so, return the spanish word for that key using getVal. After the loop, return your standard phrase for non-existing words, and you should be done.
+ 3
Don't put DictionBase in front. Right now you are attempting to access getKey via the class not the instance.
+ 2
There needs to be some sort of assiciation between an english word and spanish translations. What does that association look like?
+ 2
Ani Jona 🕊 Thank you! I will try to implement that
+ 2
So do I 👍
+ 1
That would depend on what the dictionary looks like. It appears to be a 2D array, the first index being just a number(?) and in the second dimension are words?
+ 1
You cannot compare strings using equality operator (engWord == eng). This way you are comparing memory locations - that is, you compare for identity, not just equality.
Use: engWord.equals(eng)
+ 1
Ani Jona 🕊 Thanks much! I got it to work
+ 1
Awesome 🙂
0
Ani Jona 🕊 yes, the dictionary is represented in a 2D array. translateS(str) sequentially searches for a string in the dictionary. If found, the associated data value is returned, otherwise the string “No comprehendo” is returned.
0
Ani Jona 🕊
So far, In the dct file the dictionary looks like:
My :::: No comprehendo
baby::::: No comprehendo
brother:::: No comprehendo
etc…
There is also a test.txt file that will be used as inputfile with the phrase: My baby brother can go to my father and get a cat.
0
Ani Jona 🕊 ooh. In class DictionBase, there are 2 functions: getkey and getVal.
The value returned from getKey is the english word at the position. The value returned from getVal is the spanish word at the position.
0
Ani Jona 🕊 could you please take a look at the code? I’m getting an error: non-static method getKey(int) cannot be referenced from a static context
0
Ani Jona 🕊 Thanks much! There is no longer an error. I just hope it returns the correct output
0
Amazing
0
hi
0
Bbb
0
I shall try it the same manner. 🤔