Home > Software engineering >  Morse Code conversion to English - adding a space
Morse Code conversion to English - adding a space

Time:11-09

I'm working on a morse code conversion from morse code to english. I'm stuck on one part, I need to be able to add in space when 2 spaces appear in a row on the morse code, but i'm unsure how to do this. The Rule is, Each letter appears after a space in the morse code, each space appears after 2 spaces in the morse code. Problem Is I split the array using 1 space So I'm unsure how to find out when there's 2 spaces in a row.

public static void main(String[] args) throws IOException {
InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
BufferedReader in = new BufferedReader(reader);

String line;
String newMorse = ""; 
   String selectedMorse; 
   String convertedMorse;

int x = 0;

while ((line = in.readLine()) != null) {
  
     String[] morseChars = line.split(" ");



  for (int i = 0; i < morseChars.length; i  )
   
   
       selectedMorse = morseChars[i];
       convertedMorse = decode(selectedMorse);
       newMorse = newMorse   convertedMorse;

    }
      System.out.println(newMorse);

  }
}
}

Example input: .- ...- ..--- .-- .... .. . -.-. -..-(two spaces)....- .....

Expected Output: AV2WHIECX 45

I labelled where the two spaces would go to make it easy to see.

CodePudding user response:

Try this.

static final String[] MORSE = {
    "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", "--..",
    "0", "-----", "1", ".----", "2", "..---", "3", "...--", "4", "....-", "5", ".....",
    "6", "-....", "7", "--...", "8", "---..", "9", "----.",
    ".", ".-.-.-", ",", "--..--", "?", "..--..", "'", ".----.", "!", "-.-.--", "/", "-..-.",
    "(", "-.--.", ")", "-.--.-", "&", ".-...", ":", "---...", ";", "-.-.-.", "=", "-...-",
    " ", ".-.-.", "-", "-....-", "_", "..--.-", "\"", ".-..-.", "$", "...-..-", "@", ".--.-.",
    "¿", "..-.-", "¡", "--...-",
};
static final Map<String, String> DECODE = new HashMap<>();
static {
    for (int i = 0; i < MORSE.length; i  = 2)
        DECODE.put(MORSE[i   1], MORSE[i]);
}

static final Pattern MORSE_CHAR = Pattern.compile("(\\S )\\s?");

public static String decode(String input) {
    return MORSE_CHAR.matcher(input).replaceAll(m -> DECODE.get(m.group(1)));
}

public static void main(String[] args) {
    String input = ".- ...- ..--- .-- .... .. . -.-. -..-  ....- .....";
    System.out.println(decode(input));
}

output:

AV2WHIECX 45

CodePudding user response:

Since the rule is 2 spaces equal to one space and a space is between character, you can solve it by doing two splits. One at two spaces and after that one at single space. Following is inside of main that I used assuming decode takes String representing word in morse and returns the character

        InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
        BufferedReader in = new BufferedReader(reader);
        String line = ".- ...- ..--- .-- .... .. . -.-. -..-  ....- .....";
        String newMorse = ""; 
        String selectedMorse; 
        String convertedMorse;

        while ((line = in.readLine()) != null) {
            String[] morseWords = line.split("  ");
            for (String word: morseWords) {
                String[] morseChars = word.split(" ");
                for (String morseChar: morseChars)
                    newMorse = newMorse   decode(morseChar);
                newMorse = newMorse   " ";    
            }
        }
        System.out.println(newMorse);
  • Related