Search code examples
javaarraysstringmorse-code

Two arrays or one HashMap to convert a String to Morse code?


I am trying to convert a string String input; to morse code and back. I am having a hard time figuring out which method to use and also how to use it. String I am trying to convert to morse code: SOS, which should translate to ...---..., and from morse code to English: ...---... - SOS.

One way I tried is using two arrays, String[] alpha = {A-Z0-9} and String[] morse = {morse patterns}. Then I tried splitting String input into an array, to compare each character in String input with each character in String[] alpha and storing each index in an indexArray[]. I used inputArray= input.split("", -1);

Finally with some for loops and if statements I tried to use the indexes I wanted to find of the characters of the string, to find the morse code in String[] morse.

What I tried above does not work for words, but works for one character (code below). It fails and I cannot figure out how to fix it that way. Is this even the best way to do this? Or HashMap?

Then I tried using a HashMap with the English characters as key, and morse as value.

Which way is the best way to convert English string to Morse code and Morse code to English string? Arrays or HashMap?

Arrays:

private String[] alpha = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
    "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
    "W", "X", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "0", " "};

private String[] morse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
    "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
    "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
    "-.--", "--..", ".----", "..---", "...--", "....-", ".....",
"-....", "--...", "---..", "----.", "-----", "|"};

Broken for loop I was working on and couldn't figure out how to do it:

public int[] indexOfArray(String englishInput) {
    englishArray = englishInput.split("", -1);
    for (int j = 0; j < englishArray.length; j++) {
        for (int i = 0; i < alpha.length; i++) {
            if (alpha[i].equals(englishArray[j])) {
                indexArray[i] = i;
            }
        }
    }
    return indexArray;
}

This works for just one character (character to morse code):

public int indexOfArrayOld(String englishInput) {
    for (int i = 0; i < alpha.length; i++) {
        if (alpha[i].equals(englishInput)) {
            indexOld = i;
        }
    }
    return indexOld;
}

public String stringToMorseOld(int dummyIndex) {
    String morseCo = morse[dummyIndex];
    return morseCo;
}

HashMap:

private static HashMap<String, String>; alphaMorse = new HashMap<String, String>();

static {
    alphaMorse.put("A", ".-");
    alphaMorse.put("B", "-...");
    alphaMorse.put("C", "-.-.");
    alphaMorse.put("D", "-..");
    alphaMorse.put("E", ".");
    alphaMorse.put("F", "..-.");
    alphaMorse.put("G", "--.");
    alphaMorse.put("H", "....");
    alphaMorse.put("I", "..");
    alphaMorse.put("J", ".---");
    alphaMorse.put("K", "-.-");
    alphaMorse.put("L", ".-..");
    alphaMorse.put("M", "--");
    alphaMorse.put("N", "-.");
    alphaMorse.put("O", "---");
    alphaMorse.put("P", ".--.");
    alphaMorse.put("Q", "--.-");
    alphaMorse.put("R", ".-.");
    alphaMorse.put("S", "...");
    alphaMorse.put("T", "-");
    alphaMorse.put("U", "..-");
    alphaMorse.put("V", "...-");
    alphaMorse.put("W", ".--");
    alphaMorse.put("X", "-..-");
    alphaMorse.put("y", "-.--");
    alphaMorse.put("z", "--..");
    alphaMorse.put("1", ".----");
    alphaMorse.put("2", "..---");
    alphaMorse.put("3", "...--");
    alphaMorse.put("4", "....-");
    alphaMorse.put("5", ".....");
    alphaMorse.put("6", "-....");
    alphaMorse.put("7", "--...");
    alphaMorse.put("8", "---..");
    alphaMorse.put("9", "----.");
    alphaMorse.put("0", "-----");
    alphaMorse.put(" ", "|");
}

Solution

  • Efficiency-wise i would recomend the paralel array structure. I made a demo of those methods, I am not an efficiency wizard so nested loop might not be ideal here, or i may have some other flaws.

    Anyway here the example class:

    public class MorseTranslator {
    private char[] alpha = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
            'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
            'X', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
            ' ' };//Changed this to char to save some memory and to help my methods :3
    
    private String[] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
            "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
            "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--",
            "--..", ".----", "..---", "...--", "....-", ".....", "-....",
            "--...", "---..", "----.", "-----", "|" };
    
    private String toMorse(String text) {
        char[] characters = text.toUpperCase().toCharArray();
        StringBuilder morseString = new StringBuilder();
        for (char c : characters) {
            for (int i = 0; i < alpha.length; i++) {
                if (alpha[i] == c) {
                    morseString.append(morse[i]+" ");
                    break; 
                }
            }
        }
        return morseString.toString();
    }
    
    private  String fromMorse(String text){
        String[] strings = text.split(" ");
        StringBuilder translated = new StringBuilder();
        for(String s : strings){
            for(int i=0; i<morse.length; i++){
                if(morse[i].equals(s)){
                    translated.append(alpha[i]);
                    break;
                }
            }
        }
        return translated.toString();
    }
    
    public MorseTranslator(){
        String input = "Hello there";
        String morse = toMorse(input);
        String translated = fromMorse(morse);
        System.out.println("Input: "+input);
        System.out.println("Morse: "+morse);
        System.out.println("Back From morse: "+translated);
    }
    }
    

    The code above demonstrates one possible way you can do it :- ) Hopefully that helped.