Morse Code

Modules - File Streaming, Data Structures

 

Teacher’s Notes:

=======================================

This is a fairly simple program that can be easy to write if you know what you are doing.  I used file streaming to read the morse code into a quick lookup table and then did the item by item translation.

 

You will need to create your own Morse Code file called morseCode.txt.  I have provided the contents of this file below.  I would suggest using a plain text editor (like Notepad) to do this.

 
 

Assignment:

=======================================

Morse Code

Morse code was a code developed for delivering messages during the early days of electronic communication. In Morse code, each letter of the alphabet (along with the numbers from 0 to 9) are replaced with a series of dots and dashes. The letters are always in lower case. The dots and dashes can then be signaled using a simple electric switch by engaging the switch for either a short time (a dot) or a longer time (a dash).
 
For example, the letter s is coded as three dots: …
 
In this problem, you are provided with a file called morseCode.txt that contains the character codes for Morse code. The first 26 lines are the codes for the letters a through z. The following 10 lines are the codes for 1 through 9. The last code is the code for 0.
 
Write a program that reads a single line message from the keyboard (can be more than 1 word) and outputs the Morse code translation of this message. If a space is encountered in the message, the program will output a slash ( / ) to indicate it.
 
THERE ARE NO PROMPTS, ERROR TRAPS OR RANDOM
NUMBER GENERATION REQUIRED FOR THIS PROBLEM!!
 

Sample Input 1:

welcome 2025

Sample Output 1:

. - - . . - . . - . - . - - - - - . / . . - - - - - - - . . . - - - . . . . .
 
Note that the output is shown with spaces between each dot and dash for readability. Your program output should only have a space between each letter.

Explanation of Sample Output 1:

The translations for each letter are as follows:
 
w . - -
e .
l . - . .
c - . - .
o - - -
m - -
e .
    /
2 . .- - -
0 - - - - -
2 . . - - -
5 . . . . .
 
When combined, these letters make the output message:

 

Solution (JAVA):

=======================================

import java.io.File;
import java.util.Scanner;
public class Main {
    public static void main(String[] args)throws Exception{
        
        Scanner fileInput = new Scanner(new File("morsecode.txt"));
        
        String[] letters = new String[123];
        for(int x = 97; x < 123; x++)
            letters[x] = fileInput.next();
        
        for(int x = 49; x < 57; x++)
            letters[x] = fileInput.next();
        
        letters[48] = fileInput.next();
                
        Scanner input = new Scanner(System.in);
        String message = input.nextLine();
        
        String morseCode = "";
        
        for(int x = 0; x < message.length(); x++) {
            if(message.charAt(x) == ' ')
                morseCode += "/";
            else
                morseCode += letters[message.charAt(x)] + " ";
        }
        System.out.println(morseCode);
    }
}
 

 

 

 

Morse Codes - Save into a file called morseCode.txt 

=======================================

.-
-...
-.-.
-..
.
..-.
--.
....
..
.---
-.-
.-..
--
-.
---
.--.
--.-
.-.
...
-
..-
...-
.--
-..-
-.--
--..
.----
..---
...--
....-
.....
-....
--...
---..
----.
-----