#using hashmaps to traverse hashmaps

1 messages · Page 1 of 1 (latest)

raven flume
#

Basically working on a translating program, I have created a hashmap called SpanishToEnglish which holds english translations for spanish, now the problem is I have it working but if a user wants to type in a english translation it won't work because ``` String result3 = spanishToEnglish.get(input);
String result = englishToGerman.get(result3);
String result2 = englishToFrench.get(result3);

it will only work if input is a spanish word and the other 2 need result3, is there anyway to do this so I can get a user to translate from english to other languages and from spanish to all other languages
tiny notchBOT
#

<@&987246399047479336> please have a look, thanks.

tiny notchBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

raven flume
#

    private void fillEnglishToSpanish() {
        spanishToEnglish.put("hola", "hello");
        spanishToEnglish.put("usted", "you");
        spanishToEnglish.put("cerveza", "cerveza");
        spanishToEnglish.put("table", "mesa");
        spanishToEnglish.put("silla", "chair");
        spanishToEnglish.put("yo", "i");
        spanishToEnglish.put("la/le", "the");
        spanishToEnglish.put("no poder", "cannot");
        spanishToEnglish.put("monitora", "monitor");
        spanishToEnglish.put("trabaja", "work");
        spanishToEnglish.put("embutido", "saussage");
tiny notchBOT
raven flume
#

here is my spanish to english hashmap

#

    private void fillEnglishToFrench() {
        englishToFrench.put("hello", "salut");
        englishToFrench.put("you", "vous");
        englishToFrench.put("beer", "bière");
        englishToFrench.put("table", "tableau");
        englishToFrench.put("chair", "chaise");
        englishToFrench.put("i", "je");
        englishToFrench.put("the", "la/le");
        englishToFrench.put("cannot", "ne peux pas");
        englishToFrench.put("monitor", "surveiller");
    }

    private void fillEnglishToGerman() {
        englishToGerman.put("hello", "hallo");
        englishToGerman.put("saussage", "wurst");
        englishToGerman.put("beer", "bier");
        englishToGerman.put("table", "tisch");
        englishToGerman.put("chair", "stuhl");
        englishToGerman.put("i", "ich");
        englishToGerman.put("you", "du/sie");
        englishToGerman.put("the", "der/die/das");
        englishToGerman.put("work", "arbeit");  ```
tiny notchBOT
raven flume
#

others for contex

#

context *

#

is there a way to put 2 values in the .get

#

so i could put like .get(input OR result3)

#

something like that?

sour stump
#

I dont quite get what you want, sorry

raven flume
#

basically

#

need to translate from spanish to english, then english to german and french

#

sorry for being confusing

#

thats it summed up

calm sun
#

The JDK does not have this but you can use Google Guava's BiMap

raven flume
#

gotta use hashmaps

sour stump
#

you can make multiple maps

#

that is how you would usually do that

calm sun
sour stump
#

EN to GER, GER to EN, EN to FR, FR to EN....

raven flume
#

can I do that with hashmaps?

sour stump
raven flume
#

english

#

then create a map with 3 values?

sour stump
#

and have a hash map that translates from this language into every other and one for every other to translate into this

#

like you pick english

raven flume
#

i've done that have I not

sour stump
#

you would need to translate from GER to EN, FR to EN, SP to EN
And you would do EN to GER, EN to FR and EN to SP

raven flume
#

right, so multiple hashmaps with keys and values switched

sour stump
#

yes

raven flume
#

is there any way

#

to do it with the 3 i have?

sour stump
#

spanish to english, english to german and english to french?

raven flume
#

yeah,

lean wharf
#

Create a map for each language conversion

sour stump
#

those 3 will only ever be able to translate spanish to english, german or french or english to german or french

#

if you want to go backwards, you will need new maps

raven flume
#

right

#

is there any possibility

#

I could make a if statement

#

such as if the user input = spanish word, it uses ```String result3 = spanishToEnglish.get(input);
String result = englishToGerman.get(result3);
String result2 = englishToFrench.get(result3);

sour stump
#

you can chain them with the get method

raven flume
#

and if user input = english word ```String result3 = spanishToEnglish.get(input);
String result = englishToGerman.get(input);
String result2 = englishToFrench.get(input);

tiny notchBOT
raven flume
#

chain them?

sour stump
#

englishToGerman.get(spanishToEnglish.get("Holla")));

raven flume
#

ok

#

Right

#

that does work

#

is there a way

#

wait no

#

its inpractiacle

#

since i have a tonne of keys

sour stump
#

just ask 😄

raven flume
#

I'm just so stuck...

#

i need to make this work

#

with 3 hashmaps

#

I could do it with other things, but it has to be 3 hashmaps for some odd reason

sour stump
#

what is the task anyway?

#

are the 3 hashmaps given?

raven flume
#

create a translation program

#

no

#

created them

sour stump
raven flume
#

as in specific languages/

#

?*

sour stump
#

no what exactly is your task?

raven flume
#

theres been 3 tasks

#

1 was to create it and make it so you could translate from english into french

#

then french and german for task 2 with unqiue words

#

now im on task 3

#

which I've got working

#

but it means task 1 + 2 is not working

sour stump
#

and task 3 is?

raven flume
#

translating from spanish to english, then english to german/frenvh

sour stump
#

btw what is the language you study in? french, german, english, spanish?

raven flume
#

english

#

main language

sour stump
#

then your professor probably wants you to use english as a default

raven flume
#

yup

#

exactly,

#

but he wants us to achieve this with 3 hashmaps

sour stump
#

so your tasks are:

  1. create a translation program that translates from english to french
  2. translate from french to german
  3. translate from spanish to english and from english to german or french
#

and do all that with 3 hashmaps

raven flume
#

nono, just english to french + german

#

only from spanish into the other languages

sour stump
#

then copy my message and correct it please, because I am confused

raven flume
#

thats all to be done with 3 hashmaps

raven flume
#

thats to be done with 3 hashmaps

sour stump
#

how do you translate from english to french AND german at the same time?

#

spanish to english
english to french
english to german
with these 3 maps you should be able to do all of them.

raven flume
#

right,

#

which

#

it does work

#

but it doesn't work if a user wants to translate a english word

#

into french or german

sour stump
#

well yes, you got a hash map that deals with english words

#

all you need to do is access the right map

calm sun
#

It will work but you will have to traverse through the values() of the map

sour stump
#

but you cannot traverse a hashmap

#

all you can do is use the containsKey() method

raven flume
#

and then manually

#

write each one?

calm sun
#

sure you can. map.values() returns a collection

raven flume
#

does map. work with hashmap?

calm sun
#

it won't be as fast as a hashmap

#

yep, it is part of the Map interface

sour stump
#

if it is that what you want, containsKey() does it faster. 🤷‍♂️

#

or rather easier to write

calm sun
#

it would be containsValue() right?

#

but that just returns boolean, then you have to find it

sour stump
#

in englishtoGerman the input would be a word in english. to check if you even have that in your map you use containsKey().

ruby coral
#

here is a way

sour stump
#

you can check all 3 of them and translate.

calm sun
#

i thought the question was searching for the value instead

#

containsKey() would only give you one side

ruby coral
#
enum Language {
   EN,
   DE,
   FR
}

HashMap<String, HashMap<Language, String>> translationMap = new HashMap<>();
sour stump
calm sun
#

sounds like they want to search for Hello or Hola or ???

sour stump
#

if practice the user would need to select the language they enter and the language they want. there are just too many words in several languages with different meanings in either.

sour stump
#

that is fancy, but I think it is too high level for OP

ruby coral
#

there is a "key"

#

so like "hello"

#

and then you look up how to say that thing in the language of your choice

ruby coral
#

ah okay nvm

#

his requirements are different

#

interesting

sour stump
raven flume
#

yeah

#

.containKey?

#

.Contain

sour stump
#

there is something missing from the bottom

raven flume
#

yeah just gettnig it

sour stump
#

ohhh

sour stump
#

well, thanks for finally posting it 😄

#

finding the right hashmap was never the issue since you are supposed to use them all sequencially anyway

#

and make 3 printout statements

raven flume
#
if(result !=  null && result2 != null && result3 != null ) {
            return "GERMAN: " + result + "\nFRENCH: " + result2 + "\nENGLISH: " + result3; }
        
        else if (result != null && result2 != null) {
            return "GERMAN: " +  result + "\nFRENCH: " +  result2; }
        
        else if (result2 != null && result3 != null) { 
            return "FRENCH: " + result2 + "\nENGLISH: " + result3; }
        
        else if (result != null && result3!= null) {
            return "GERMAN: " +  result + "\nENGLISH: " + result3;
        }
        
        else if (result != null ) {
            return "GERMAN: " +  result; }
        
        else if (result2 != null ) {
            return "FRENCH: " + result2;}
        
        else if (result3 != null) {
            return "ENGLISH: " + result3; }
tiny notchBOT
raven flume
#

my print statements lmao

#

extremely messy

#

and probably the worst possible way you could do it

sour stump
#

well you can start by renaming the results properly so you see what they are at a glance

#

other than that. why do you check for null so often?

raven flume
#

so that when it displays I don't get a null message

#

if there is no translation

#

since we had to use unique words

sour stump
#

you make the maps, you make the translations. Check if spanishToEnglish.containsKey(input) that returns a boolean, if true, there will not be a null error

#

if you make sure to translate every word in every language, there will be no other null problems

raven flume
#

but spanishToEnglish.containsKey(input)

#

how does it work,

#

so a if (spanishToEnglish.containsKey(input)) {

#
  return "GERMAN" + result }
#

like that?

sour stump
#

if it contains the key, do all your print outs

#

no need for aditional if statements

ruby coral
#

so your problem is actually a graph problem btw

sour stump
#

just make sure you translate every spanish key into every language and enter it in every hashmap

raven flume
#

right wait

#

how do I do that

#

spanish key into every language

#

can i make a hashmap have 3 values?

#

wait no

sour stump
#

you take a spanish word, translate it into english and put it into the map. then you take the english word and translate it into german and french and make a corresponding entry in the other maps

raven flume
#

right,

#

but im confused

#

what do you mean put it into the map?

#
        spanishToEnglish.put("hola", "hello");
        spanishToEnglish.put("usted", "you");
        spanishToEnglish.put("cerveza", "cerveza");
        spanishToEnglish.put("table", "mesa");
        spanishToEnglish.put("silla", "chair");
        spanishToEnglish.put("yo", "i");
        spanishToEnglish.put("la/le", "the");
        spanishToEnglish.put("no poder", "cannot");
        spanishToEnglish.put("monitora", "monitor");
        spanishToEnglish.put("trabaja", "work");
        spanishToEnglish.put("embutido", "saussage");
        
tiny notchBOT
sour stump
#

I mean use the put method to extend the map with a new translation pair

#

spanishToEnglish.put("embutido", "saussage");

#

like this

raven flume
#

I can't find any thing online on how to extend a hashmap with user input

sour stump
#

why would you want to extend it with user input?

raven flume
#

sorry im super confused

#

havent' I already done this?

sour stump
#

done what?

raven flume
#

you take a spanish word, translate it into english and put it into the map. then you take the english word and translate it into german and french and

#

""

#

i've done that

sour stump
#

then I do not know what you even want.

#

you know how to do it

#

you know the syntax

#

what is your question?

raven flume
#

time to maybe rephrase the question,

#

is there a way make like a if statement

#

if the user chooses a english word to translate

#

or a spanish word to translate

sour stump
#

depends if the user will select a language or if you want to derive it from the input

raven flume
#

derive from input

sour stump
#

then you can make something like this

#
if (englishToGerman.containsKey(input)) {
//do the translation
} else if (spanishToEnglish.containsKey(input)) {
//do other translation
}```
tiny notchBOT
raven flume
#

yeah

#

thats what im doing

#

rn

#

right

#

ok good

#

this might be it

sour stump
#

😄

#

always be clear on what you want.

#

this can be done very quickly

raven flume
#

this couldve been done way quicker if i wasn't forced to use 3 flipping hashmaps

#

right

#

thats it

#

Thank you so much man

#

apologies for wasting ur time

#

and being complicated

#
        if (spanishToEnglish.containsKey(input))
        {
            String result3 = spanishToEnglish.get(input);
            String result = englishToGerman.get(result3);
            String result2 = englishToFrench.get(result3);
            
            return "GERMAN: " + result + "\nFRENCH: " + result2 + "\nENGLISH: " + result3; } 
        
        else if (englishToGerman.containsKey(input))
        {
            
            String result = englishToGerman.get(input);
            String result2 = englishToFrench.get(input);
            
            return "GERMAN: " + result + "\nFRENCH: " + result2; }
        
        else if (englishToGerman.containsKey(input))
        { 
            String result = englishToGerman.get(input);
            String result2 = englishToFrench.get(input);
            return "GERMAN: " + result + "\nFRENCH: " + result2; }
tiny notchBOT
raven flume
#

final code

#

really do appreciate it