import
java.util.HashMap;
public
class
MorseCodeTranslator {
public
static
String morseToText(String morseCode)
{
HashMap<String, String> morseCodeDict
=
new
HashMap<>();
morseCodeDict.put(
".-"
,
"A"
);
morseCodeDict.put(
"-..."
,
"B"
);
morseCodeDict.put(
"-.-."
,
"C"
);
morseCodeDict.put(
"-.."
,
"D"
);
morseCodeDict.put(
"."
,
"E"
);
morseCodeDict.put(
"..-."
,
"F"
);
morseCodeDict.put(
"--."
,
"G"
);
morseCodeDict.put(
"...."
,
"H"
);
morseCodeDict.put(
".."
,
"I"
);
morseCodeDict.put(
".---"
,
"J"
);
morseCodeDict.put(
"-.-"
,
"K"
);
morseCodeDict.put(
".-.."
,
"L"
);
morseCodeDict.put(
"--"
,
"M"
);
morseCodeDict.put(
"-."
,
"N"
);
morseCodeDict.put(
"---"
,
"O"
);
morseCodeDict.put(
".--."
,
"P"
);
morseCodeDict.put(
"--.-"
,
"Q"
);
morseCodeDict.put(
".-."
,
"R"
);
morseCodeDict.put(
"..."
,
"S"
);
morseCodeDict.put(
"-"
,
"T"
);
morseCodeDict.put(
"..-"
,
"U"
);
morseCodeDict.put(
"...-"
,
"V"
);
morseCodeDict.put(
".--"
,
"W"
);
morseCodeDict.put(
"-..-"
,
"X"
);
morseCodeDict.put(
"-.--"
,
"Y"
);
morseCodeDict.put(
"--.."
,
"Z"
);
morseCodeDict.put(
"-----"
,
"0"
);
morseCodeDict.put(
".----"
,
"1"
);
morseCodeDict.put(
"..---"
,
"2"
);
morseCodeDict.put(
"...--"
,
"3"
);
morseCodeDict.put(
"....-"
,
"4"
);
morseCodeDict.put(
"....."
,
"5"
);
morseCodeDict.put(
"-...."
,
"6"
);
morseCodeDict.put(
"--..."
,
"7"
);
morseCodeDict.put(
"---.."
,
"8"
);
morseCodeDict.put(
"----."
,
"9"
);
morseCodeDict.put(
"/"
,
" "
);
StringBuilder translatedText =
new
StringBuilder();
String[] words = morseCode.split(
"/"
);
for
(String word : words) {
String[] letters = word.split(
" "
);
for
(String letter : letters) {
translatedText.append(
morseCodeDict.getOrDefault(letter,
""
));
}
translatedText.append(
" "
);
}
return
translatedText.toString()
.trim();
}
public
static
void
main(String[] args)
{
String morseCodeInput
=
"-- --- .-. ... . / -.-. --- -.. . / .. ... / ..-. --- .-. --. . - - .- -... .-.. ."
;
System.out.println(
"Morse Code: "
+ morseCodeInput);
System.out.println(
"English Text: "
+ morseToText(morseCodeInput));
}
}