Morse Code
- Yahvin Gali
- Mar 18, 2021
- 3 min read
Updated: Sep 12, 2021
To convert user input from English language to Morse Code, and display the resulting translation.
The History of Morse
Morse Code was system of binary code first used in 1840, when Samuel B. Morse invented the Telegraph. Prior to this time, communication was solely through manual deliver, via oversea travel, horseback, or message runners. With the introduction of the telegraph, communication became faster than ever, nearly instant in fact.
The first telegraph messages were sent over lines strung between Washington DC and Baltimore, in accordance to a system of short or long electrical pulses, commonly known as dits and dahs.
While this system is no longer used for communication, it is has been a topic of interest for adaptive communication technology for disabled persons.
Using a system of shot and long electrical pulses, along with the occasional pause, society was capable of communicating like never before.
Introduction
This program follows the convention of the International Morse Code developed in 1844. For the sake of clarity and understanding, the entire alphabet of Morse Code used for this project is shown below.

Constructing a Pattern
Before constructing a Morse Code generator, the English alphabet must first be "synced" with the Morse Code alphabet. To do so, the englishConvert() method was created. In this method, the Morse Code Alphabet was read in from a .txt file using a Scanner class object. The code is shown below:
String[] code = new String[36];
ArrayList<String> lines = new ArrayList<String>();
File file = new File("morseCode.txt");
Scanner sc = new Scanner(file);
while (sc.hasNext()) {
String str = sc.nextLine();
lines.add(str);
}
sc.close();
The contents of the text file is shown below:
.-
-...
-.-.
-..
.
..-.
--.
....
..
.---
-.-
.-..
--
-.
---
.--.
--.-
.-.
...
-
..-
...-
.--
-..-
-.--
--..
.----
..---
...--
....-
.....
-....
--...
---..
----.
-----
Once the Morse Code is read in and added to the lines ArrayList as a sequence of Strings, a for-loop is utilized to add the same Strings to an array. This was done to allow better use of array functions and prevent any discrepancies between types of storage, since the English alphabet is also stored in an array. Both the for-loop and the English alphabet array are shown below:
public static String englishConvert(String english) throws Exception{
String[] array = new String[english.length()];
String[] letter = { "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"};
...
for (int i = 0; i < code.length; i++){
code[i] = lines.get(i);
}
The method englishConvert throws an Exception of FileNotFoundException. Essentially, if the file is not found, rather than the program ending with a compile error, a simple output statement is displayed, that explains the file does not exist in the specified file location.
Translation
Now that both the English and Morse Code alphabets are stored in arrays, the inputted phrase that needs to be translated is stored in a third array. This is done by storing each letter as an individual String via the inherent java substring() method.
String result = "";
for (int i = 0; i<english.length(); i++){
array[i] = english.substring(i,i+1);
}
Lastly, a nested for-loop is used to cross-refernce each letter of the to-be-translated phrase to the English alphabet. If the letter in the phrase matches the English character, then the related Morse Code character is added into the String result. The nested for loop can be viewed on the Github Repository for this program.
Accepting User Input
The last part of the project entails using a Tester class, MorseCodeTester, to accept an English phrase as user input. This done, once again, with a Scanner class object. The program will continue to run as long as the user types "Y" when prompted to do so. If "N" is entered, the program ends. An example run is shown below (User input is bolded:
Enter 1 to Use
Enter 2 to Quit
1
Enter a prompt in english:
hello
Morse Code: .... . .-.. .-.. ---
Continue? (Y/N)
Y
Enter a prompt in english:
bye
Morse Code: -... -.-- .
Continue? (Y/N)
N
Have a nice day.
Final Thoughts
This project was quite engaging and fun. While I did not encounter any particularly difficult section of code, I did enjoy both the programming and learning aspects of this program. Not only did I experience a quick history lesson, but I was also able to create my first converter/translator program. I am sure the java techniques I learn in this project will serve me well in the future.
P.S. - They did! My next project, the Caesar Shift Cipher post, is both an encryption and decryption program.
Comments