Search code examples
javastringloopsswitch-statementmorse-code

I want to translate morse code into words. Still have a small problem


So Im splitting at " ". Problem is that morsecode is dividing each word by three spaces. " ".

So when Im translating it always prints out each word written together without spaces inbetween.

Heres what I have. Im using StringBuilder for obvious reasons.

public class MorseCodeDecoder {
public static String decode(String morseCode) {
    String word = "";
    String character = "";

    //count how much space is inbetween lines.
    StringBuilder codeTwo = new StringBuilder();
    String[] output = morseCode.split(" ");


    for (int i = 0; i < output.length; i++) {

Inside here I tried to do an seperate if-statement. if morseCode contains three spaces back to back append one space.

//if(morseCode.contains("   ")) codeTwo.append(" "); (or \\s maybe, doesnt matter)


        switch (output[i]) {
            case "   ":
                codeTwo.append(" ");
                break;
            case ".-":
                codeTwo.append("A");
                break;
            case "-...":
                codeTwo.append("B");
                break;
            case "-.-.":
                codeTwo.append("C");
                break;
            case "-..":
                codeTwo.append("D");
                break;
            case ".":
                codeTwo.append("E");
                break;
            case "..-.":
                codeTwo.append("F");
                break;
            case "--.":
                codeTwo.append("G");
                break;
            case "....":
                codeTwo.append("H");
                break;
            case "..":
                codeTwo.append("I");
                break;
            case ".---":
                codeTwo.append("J");
                break;
            case "-.-":
                codeTwo.append("K");
                break;
            case ".-..":
                codeTwo.append("L");
                break;
            case "--":
                codeTwo.append("M");
                break;
            case "-.":
                codeTwo.append("N");
                break;
            case "---":
                codeTwo.append("O");
                break;
            case ".--.":
                codeTwo.append("P");
                break;
            case "--.-":
                codeTwo.append("Q");
                break;
            case ".-.":
                codeTwo.append("R");
                break;
            case "...":
                codeTwo.append("S");
                break;
            case "-":
                codeTwo.append("T");
                break;
            case "..-":
                codeTwo.append("U");
                break;
            case "...-":
                codeTwo.append("V");
                break;
            case ".--":
                codeTwo.append("W");
                break;
            case "-..-":
                codeTwo.append("X");
                break;
            case "-.--":
                codeTwo.append("Y");
                break;
            case "--..":
                codeTwo.append("Z");
                break;
        }
    }
    return codeTwo.toString();
  }
 }

For input: .... . -.-- .--- ..- -.. . Expected output: "HEY JUDE"


Solution

  • You can use regex instead of splitting by spaces... Something like this pattern:

    ([._])+\s*
    

    Explanation: We're looking for at least 1 instance of a dot (.) or underscore (_) followed by zero or more spaces...

    I've used it over this example:

    ._ _... _._   .._ ..._ .__
    

    Which is: ABC UVW

    I'm not near a computer - but the program should look something like that:

    public static String decode(final String morseCode) {
        final Matcher matcher = pattern.matcher(morseCode);
    
        final StringBuilder builder = new StringBuilder();
        while(matcher.find()) {
            builder.append(getLetter(matcher.group().trim()));
        }
    
        return builder.toString();
    }
    

    Assuming getLetter() is your switch case.

    More of a nitpick - instead of using this massive switch case you should use a static map that will contain the mapping between the symbols and letters and initialize it on the static constructor ( https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html )

    Hope that helps.