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.
Try this.
output: