I want to build a program that translates English to Morse code and visa versa, I have decided to use hash maps to do this, but I'm unsure as to how I could run the string through the hash map and get the translation out at the end. Here is my code at the moment:
import java.util.HashMap;
import java.util.Map;
public class MorseCodeTranslator{
public static String translateToMorseCode() {
String englishtoMorse = "";
String translation = null;
Map<Character, String> morse = new HashMap<Character, String>();
morse.put('a', "._");
morse.put('b', "_...");
morse.put('c', "_._");
morse.put('d', "_..");
morse.put('e', ".");
morse.put('f', ".._.");
morse.put('g', "__.");
morse.put('h', "....");
morse.put('i', "..");
morse.put('j', ".___");
morse.put('k', "_.");
morse.put('l', "._..");
morse.put('m', "__");
morse.put('n', "_.");
morse.put('o', "___");
morse.put('p', ".__.");
morse.put('q', "__._");
morse.put('r', "._.");
morse.put('s', "...");
morse.put('t', "_");
morse.put('u', ".._");
morse.put('v', "..._");
morse.put('w', ".__");
morse.put('x', "_.._");
morse.put('y', "_.__");
morse.put('z', "__..");
return translation;
}
public static String translateFromMorseCode() {
String morsetoEnglish = "";
String translation = null;
Map<Character, String> morse = new HashMap<Character, String>();
morse.put('a', "._");
morse.put('b', "_...");
morse.put('c', "_._");
morse.put('d', "_..");
morse.put('e', ".");
morse.put('f', ".._.");
morse.put('g', "__.");
morse.put('h', "....");
morse.put('i', "..");
morse.put('j', ".___");
morse.put('k', "_.");
morse.put('l', "._..");
morse.put('m', "__");
morse.put('n', "_.");
morse.put('o', "___");
morse.put('p', ".__.");
morse.put('q', "__._");
morse.put('r', "._.");
morse.put('s', "...");
morse.put('t', "_");
morse.put('u', ".._");
morse.put('v', "..._");
morse.put('w', ".__");
morse.put('x', "_.._");
morse.put('y', "_.__");
morse.put('z', "__..");
return translation;
}
}
I want to be able to run whatever is in englishtoMorse
or morsetoEnglish
through the hash map and convert the characters to the value they are associated with in the hash map then output them in translation
.