How do I search for a String in a dictionary and return the associated value? (Language Translator) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

2nd Nov 2022, 1:04 AM
Triz
Triz - avatar
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?
2nd Nov 2022, 6:03 AM
Ani Jona šŸ•Š
Ani Jona šŸ•Š - avatar
+ 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.
2nd Nov 2022, 6:21 AM
Ani Jona šŸ•Š
Ani Jona šŸ•Š - avatar
+ 3
Don't put DictionBase in front. Right now you are attempting to access getKey via the class not the instance.
2nd Nov 2022, 7:39 AM
Ani Jona šŸ•Š
Ani Jona šŸ•Š - avatar
+ 2
There needs to be some sort of assiciation between an english word and spanish translations. What does that association look like?
2nd Nov 2022, 5:46 AM
Ani Jona šŸ•Š
Ani Jona šŸ•Š - avatar
+ 2
Ani Jona šŸ•Š Thank you! I will try to implement that
2nd Nov 2022, 6:25 AM
Triz
Triz - avatar
+ 2
So do I šŸ‘
2nd Nov 2022, 7:54 AM
Ani Jona šŸ•Š
Ani Jona šŸ•Š - avatar
+ 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?
2nd Nov 2022, 5:25 AM
Ani Jona šŸ•Š
Ani Jona šŸ•Š - avatar
+ 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)
3rd Nov 2022, 6:17 AM
Ani Jona šŸ•Š
Ani Jona šŸ•Š - avatar
+ 1
Ani Jona šŸ•Š Thanks much! I got it to work
3rd Nov 2022, 6:22 AM
Triz
Triz - avatar
+ 1
Awesome šŸ™‚
3rd Nov 2022, 6:32 AM
Ani Jona šŸ•Š
Ani Jona šŸ•Š - avatar
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.
2nd Nov 2022, 5:37 AM
Triz
Triz - avatar
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.
2nd Nov 2022, 5:51 AM
Triz
Triz - avatar
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.
2nd Nov 2022, 6:14 AM
Triz
Triz - avatar
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
2nd Nov 2022, 7:12 AM
Triz
Triz - avatar
0
Ani Jona šŸ•Š Thanks much! There is no longer an error. I just hope it returns the correct output
2nd Nov 2022, 7:45 AM
Triz
Triz - avatar
0
Amazing
2nd Nov 2022, 3:50 PM
ASIM FARHEEN ā„ļøāš”āš”šŸ¤³āš”āš”ā„ļø
ASIM FARHEEN ā„ļøāš”āš”šŸ¤³āš”āš”ā„ļø - avatar
0
hi
3rd Nov 2022, 12:21 AM
Rezaul karim
Rezaul karim - avatar
0
Bbb
3rd Nov 2022, 4:24 PM
Alex Nurnobi
Alex Nurnobi - avatar
0
I shall try it the same manner. šŸ¤”
3rd Nov 2022, 4:55 PM
ASIM FARHEEN ā„ļøāš”āš”šŸ¤³āš”āš”ā„ļø
ASIM FARHEEN ā„ļøāš”āš”šŸ¤³āš”āš”ā„ļø - avatar