0

I have a project where I need to get an input from the user, convert that to morse code and vice versa.

I have to use a hashmap, and my code looks like this. It's not really working. I'm having trouble understanding how I can print the input I get from the user on the class engToMorse.

I tried to look through other similar questions as well, but I couldn't find anything that could solve my issue.

Edit 1: By changing .toLowerCase to .toUpperCase, it does work, but only for one word. How would i go about making it work for multiple words, like a sentence. Edit2: That was fixed by adding translator.put(' ', " ");. How would I go about converting morse to english now? It is the same idea?

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(' ', "   ");
    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(translations);
    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++;
    }
    
NoobsCool
  • 57
  • 8
  • 1
    You are converting the user input to lowercase with `toLowerCase()` but your map only contains values for uppercase like `'A'`. You'll need to be consistent here. Either convert the user input to upper case or have your map map lower case chars as keys. – OH GOD SPIDERS May 03 '21 at 16:25
  • I tested it by literally just changing `.toLowerCase();` in your `engToMorse` method to `.toUpperCase();` and it works just like you would expect it (Turning sos into ...---...). If you get `null` you are still trying to look up chars that do not exist in your map and did not fix the inconsistency issues i mentioned. – OH GOD SPIDERS May 03 '21 at 16:35
  • So sorry, now I get what you're saying, it does work but only for one word. How would I go about making it work with as many words as a user can enter? – NoobsCool May 03 '21 at 16:41
  • So your only problem is that it still maps spaces to `null`? Then just add a mapping for space to your map: `translations.put(' ', " ");` – OH GOD SPIDERS May 03 '21 at 16:45
  • @OHGODSPIDERS works like a charm mate. The only issue now is the code from morse to english. I tried the same logic but obviously it doesn't work. Any ideas? – NoobsCool May 03 '21 at 17:25

1 Answers1

0

Your hashmap translations have key is upper case and you was transform all character in your "sentence" to lower case. Change it back to upper case when you get the element in hashmap is the easiest way.

while(i<sentence.length()){
    System.out.println(translations.get(Character.toUpperCase(sentence.charAt(i))));
    i++;
}
Dattq2303
  • 302
  • 2
  • 13
  • Did that but it's still showing null as the answer. It shows four seperate lines of null. – NoobsCool May 03 '21 at 16:32
  • `sentence.charAt(i)` will return a primitive `char` and you cannot call any methods on it including `toUpperCase` – OH GOD SPIDERS May 03 '21 at 16:37
  • I test it again and realize you are using character as the key. The correct way is System.out.println(translations.get(Character.toUpperCase(sentence.charAt(i)))); – Dattq2303 May 03 '21 at 16:42
  • Thanks a lot, I edited the main post as well. Now the only problem is to get the code from Morse to English, any ideas? @Dattq2303 – NoobsCool May 03 '21 at 21:40
  • @NoobsCool I believe your problem was answered in https://stackoverflow.com/questions/30903708/morse-code-to-english. Please give me correct answer if my answer helpful for you. – Dattq2303 May 04 '21 at 01:28
  • 1
    Not quite the answer I was looking for on the second one but you did answer my first problem and thank you a lot for that. I gave it the "The answer is useful" and marked it as the right answer. Cheers mate^^ – NoobsCool May 04 '21 at 12:38