-2

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++;
    }
   
    } 
HDPerson
  • 1
  • 2
  • What does the input look like? – Daniel Cheng May 04 '21 at 14:08
  • Oh sorry, I didn't add that part in the code, it should be a scanner, I'll edit it and add it right now. The user has to input the morse code and it should be converted to eng. Let me edit it right now – HDPerson May 04 '21 at 14:10
  • I think the solution will generally be splitting the input string by space and for each element, look up the morse code (key) to character (value) map and print the value. – Daniel Cheng May 04 '21 at 14:13
  • I'm a little unclear when you say split the input string. Could you elaborate on that part please? @DanielCheng – HDPerson May 04 '21 at 14:17
  • I am not convinced this question belongs to StackOverflow, as this is pretty much a question about `how to implement this very specific thing?`, instead of `how does something work`, `why doesn't it work`, or `how to use something`. Your question can be generalised in other words: `How to search strings in a list` or `How to create a transaltion table in Java`, and arguably you could find a lot of answers for this online already. – Vitor Hugo Schwaab May 05 '21 at 09:53
  • In any case, @DanielCheng pointed you to the right direction. If the user inputs `... --- ...` you could split the string using the space character, and take each of the three short strings to search for your translation. Spliting the string: https://stackoverflow.com/questions/7899525/how-to-split-a-string-by-space – Vitor Hugo Schwaab May 05 '21 at 09:56

2 Answers2

0

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
Daniel Cheng
  • 820
  • 5
  • 14
0

You can simply get morse code one by one, get one morse code in each line, process it and concat it to result string, and if you want get several morse code in one line, use a seperator between them and then split string based on that spliter for examplea #.

so you will get --#.--#..-- then split it by:

for (String singleMorse : morse.split("#")) {
        System.out.print(reversedHashMap.get(singleCode));
    }

Caution: Becouse you have SPACE in you morse code its simpler that use an splitter other than SPACE