I have a school project where I need to make a morse to english transaltor. I'm a newbie when it comes to Java so I was wondering if you could assist me. The problem is that I'm unsure on what I'm supposed to put on the while loop to turn the morse code to english. Any help would be appreciated. Thanks^^
public static void main(String[]args){
HashMap<Character,String> translations=new HashMap<Character,String>();
translations.put('A', ".-");
translations.put('B', "-...");
translations.put('C', "-.-.");
translations.put('D', "-..");
translations.put('E', ".");
translations.put('F', "..-.");
translations.put('G', "--.");
translations.put('H', "....");
translations.put('I', "..");
translations.put('J', ".---");
translations.put('K', "-.-");
translations.put('L', ".-..");
translations.put('M', "--");
translations.put('N', "-.");
translations.put('O', "---");
translations.put('P', ".--.");
translations.put('Q', "--.-");
translations.put('R', ".-.");
translations.put('S', "...");
translations.put('T', "-");
translations.put('U', "..-");
translations.put('V', "...-");
translations.put('W', ".--");
translations.put('X', "-..-");
translations.put('Y', "-.--");
translations.put('Z', "--..");
translations.put('0', "-----");
translations.put('1', ".----");
translations.put('2', "..---");
translations.put('3', "...--");
translations.put('4', "....-");
translations.put('5', ".....");
translations.put('6', "-....");
translations.put('7', "--...");
translations.put('8', "---..");
translations.put('9', "----.");
translations.put(' ', " ");
HashMap<String, Character> reversedHashMap = new HashMap<String, Character>();
for (char key : translations.keySet()){
reversedHashMap.put(translations.get(key), key);
}Scanner scan=new Scanner(System.in);
System.out.println("Welcome to the translator. Type 1 for English to Morse or type 2 for Morse to English: ");
int choice=scan.nextInt();
if(choice==1)
engToMorse(translations);
else if(choice==2)
morseToEng(reversedHashMap);
else{
System.out.println("Invalid Input!");
}
}
public static void engToMorse(HashMap<Character,String> translations){
Scanner scan=new Scanner(System.in);
System.out.println("Please enter the sentence that you want to translate to Morse here: ");
String sentence=scan.nextLine().toUpperCase();
int i=0;
while(i<sentence.length()){
System.out.printf(translations.get(sentence.charAt(i)));
i++;
}
}
public static void morseToEng(HashMap<String,Character> reversedHashMap){
Scanner scan=new Scanner(System.in);
System.out.println("Please enter the morse code that you want to translate to english: ");
String morse=scan.nextLine().toUpperCase();
int i=0;
while(i<morse.length()){
System.out.print();
i++;
}
}
After you've read the input, split the input string by space. For each element, look up the morse code (key) to character (value) map and print the value.
for (String singleCode : morse.split(" ")) {
System.out.print(reversedHashMap.get(singleCode));
}
Sample input output
Welcome to the translator. Type 1 for English to Morse or type 2 for Morse to English:
2
Please enter the morse code that you want to translate to english:
... -- ...
SMS