I created a morse converter from a .txt file.
I'm learning java and I created a simple morse converter. Convert only A to Z and 0 to 9 chars. Space char remain for split words in morse-code. At the beginning I used a big switch for any letters or number but it was no good.
public static String toMorse(byte[] bytes) {
String text = new String(bytes);
String morseText = "";
text = text.toLowerCase();
String[] morseChar
= {".-",
"-...",
"-.-.",
"-..",
".",
"..-.",
"--.",
"....",
"..",
".---",
"-.-",
".-..",
"--",
"-.",
"---",
".--.",
"--.-",
".-.",
"...",
"-",
"..-",
"...-",
".--",
"-..-",
"-.--",
"--.."};
String[] morseNumber
= {".----",
"..---",
"...--",
"....-",
".....",
"-....",
"--...",
"---..",
"----.",
"-----"};
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) >= 'a' && text.charAt(i) <= 'z') {
morseText = morseText + morseChar[text.charAt(i) - 'a'];
} else if (text.charAt(i) >= '0' && text.charAt(i) <= '9') {
morseText = morseText + morseNumber[text.charAt(i) - '0'];
} else if (text.charAt(i) == ' ') {
morseText = morseText + text.charAt(i);
}
}
return morseText;
}