Getting values from HashMap according to user input / morse code generator

I am trying to create a Java program that takes a users string input and translates it into morse code. I am attempting to store each letter of the alphabet in a hashMap with its corresponding morse code & my goal is to be able to get the value (morse code) when the key(regular letter) is read from the input. Ive attempted something below but it keeps printing null when I test it out. I am fairly new to java and can't seem to figure out how to do this.

import java.util.HashMap;
import java.util.Scanner;

public class Alphabet{

public static void main(String args[]) {

    Scanner sc = new Scanner(System.in);

    HashMap morsecode = new HashMap();
    morsecode.put("a",",-");
    morsecode.put("b","-...");
    //will add other letters later

    System.out.println("please enter an english sentence:");
    String val = (String)morsecode.get(sc.nextLine());
    System.out.println(val);        


}
}

2 answers

  • answered 2018-02-22 06:25 Elliott Frisch

    As I said in my initial comment, please don't use raw types. Here your Map should probably be of Character, String because that is why you can't currently map multiple characters in a line to single characters in your Map. Basically, I would do it something like

    Scanner sc = new Scanner(System.in);
    
    Map<Character, String> morsecode = new HashMap<>();
    morsecode.put('a', ",-");
    morsecode.put('b', "-...");
    // will add other letters later
    
    System.out.println("please enter an english sentence:");
    String line = sc.nextLine();
    for (char ch : line.toLowerCase().toCharArray()) {
        System.out.print(morsecode.get(ch) + " ");
    }
    System.out.println();
    

    But you could also do

    Scanner sc = new Scanner(System.in);
    
    Map<String, String> morsecode = new HashMap<>();
    morsecode.put("a", ",-");
    morsecode.put("b", "-...");
    // will add other letters later
    
    System.out.println("please enter an english sentence:");
    String line = sc.nextLine();
    for (char ch : line.toLowerCase().toCharArray()) {
        System.out.print(morsecode.get(Character.toString(ch)) + " ");
    }
    System.out.println();
    

    or any other method of iterating single characters from the user's input.

  • answered 2018-02-22 06:27 Christian Ramos

    Your code is printing null because this line String val = (String)morsecode.get(sc.nextline()); is retrieving the String value of, according to your comment, the String "ab". According to how you're adding entries to the HashMap, you do not add "ab" but instead add the individual letters "a" and "b"; you're asking the HashMap for something you never gave it. Below, I've added a translation step for what I think you're trying to do. The small block near the end will pull each translation you gave the HashMap and build a new String for every letter in the English sentence you gave the program.

    import java.util.HashMap;
    import java.util.Scanner;
    
    public class Alphabet
    {
        public static void main(String args[])
        {   
            Scanner sc = new Scanner(System.in);
    
            HashMap<String, String> morsecode = new HashMap<>();
            morsecode.put("a",",-");
            morsecode.put("b","-...");
            //will add other letters later
    
            System.out.println("please enter an english sentence:");
            String input = sc.nextLine();
    
            // Translate each letter from English -> code
            final StringBuilder builder = new StringBuilder();
            for (final char letter : input.toCharArray()) {
                builder.append(morsecode.get(Character.toString(letter)));
            }
    
            System.out.println(builder.toString());
            sc.close();
        }
    }