I am working on a simple Morse Code translator for my Intro to Programming class. This is a very simple design based on the techniques I have been taught.
This program works for a single character conversion, but cannot do words or sentences. I believe the problem has to do with the morse[index]
statement at the end, but I can't figure out how to print the translated text as a whole.
public class Exercise12_9
{
public static void main(String[] args)
{
String[] english = { "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",
",", ".", "?" };
String[] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".---.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
"..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
"-----", "--..--", ".-.-.-", "..--.." };
Scanner keyboard = new Scanner(System.in);
String userInput;
int index;
index = 0;
System.out.println(" This is an English to Morse Code Translator. ");
System.out.println(" Please enter what you would like translate ");
System.out.println(" into Morse Code. ");
System.out.println(" ============================================ ");
userInput = keyboard.next();
userInput = userInput.toLowerCase();
for (index = 0; index < userInput.length(); index++)
{
char [] chars = userInput.toCharArray();
if (userInput.equals(english[index]))
{
System.out.println(" Translated : " + morse[index]);
}
}
}
}
Here is the Optimized Code of your Given Solution
public class MorseCode {
public static Scanner sc;
public static void main(String args[]) throws IOException //Input Output Exception is added to cover the BufferedReader
{
int option = 0;
String sentence = "",answer = "",answer1 = "";
char[] english = { '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',
',', '.', '?' }; //Defining a Character Array of the English Letters numbers and Symbols so that we can compare and convert later
String[] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".---.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
"..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
"-----", "--..--", ".-.-.-", "..--.." }; //Defining an Array of String to hold the Morse Code value of Every English Letter,Number and Symbol in the same order as that of the character Array
sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(">>>>Welcome to MorseCode Software<<<<");
System.out.println("");
do
{
System.out.println("-->Enter the Option Corresponding to the Task you want to Perform ");
System.out.println("->1.Generate Morse Code<- OR ->2.Generate English Language<- OR ->3.Exit ");
System.out.print("->");
while(!sc.hasNextInt()) //Repeat Until the next Item is an Integer i.e Until the Next Item is an Integer Keep on Repeating it
{//NOTE- The hasnext() function is also used when we are using the Iterator where the next input is always checked and then if it is valid it is allowed to be entered
System.out.println("");
System.err.println("-->ERROR<-->Enter Digits Only<--");
System.out.print("->");
sc.next(); //Repeat and Discard the previous Inputs which are not valid
}
option = sc.nextInt();
switch(option)
{
case 1:
{
System.out.println("");
System.out.println("-->Enter the Sentence that you want to Transmit Using the Morse Code ");
System.out.print("->");
sentence = br.readLine();
System.out.println("");
sentence = sentence.toLowerCase(); //Because morse code is defined only for the lower case letters and the numbers and the Symbols will remain the Same
char[] morsec = sentence.toCharArray();
for(int i = 0; i < morsec.length;i++) //The loop will run till i is less than the number of characters in the Sentence because Every Character needs to Be Converted into the Respective Morse Code
{//For Every Letter in the User Input Sentence
for(int j = 0;j<english.length;j++) //For Every Character in the morsec array we will have to traverse the entire English Array and find the match so that it can be represented
{
if(english[j] == morsec[i]) //If the Character Present in English array is equal to the character present in the Morsec array then Only Execute
{//Always remember that the condition in the Inner loop will be the first to be Equated in the If Statement because that will change until the characters match
answer = answer + morse[j] + " "; //After Every Letter is generated in the Morse Code we will give a Space
} //Since the Letters in the English char and the symbols present in the morse array are at the Same Index
}
}
System.out.println("-->The Morse Code Translation is:- ");
System.out.print(">> ");
System.out.println(answer);
System.out.println("");
break;
}
case 2:
{
System.out.println("");
System.out.println("-->Enter the Morse Code and After Every Letter add Space in Between ");
System.out.print("-> ");
sentence = br.readLine();
System.out.println("");
String[] morsec = sentence.split(" "); //To use the split function to Convert Every Morse Code String as a Separate Entry in the STring array
for(int i = 0;i < morsec.length;i++)
{//For Every morse code Letter Entered
//Remember - We are Splitting on the Basis of the space
for(int j = 0;j < morse.length;j++)
{
if(morse[j].equals(morsec[i])) //When you are comparing the String you have to Do this and not ==
{
answer1 = answer1 + english[j]; //Since the characters in the Morse array and the English Array are in the Same Index
}
}
}
System.out.println("-->The English Language Translation is:- ");
System.out.print(">> ");
System.out.println(answer1);
System.out.println("");
break;
}
case 3:
{
System.out.println("");
System.out.println(">>Thank you For Using this Service<<");
System.out.println("");
break;
}
default:
{
System.err.println("-->ERROR<-->Invalid Option Entered<--");
System.out.println("");
break;
}
}
}
while(option!=3);
}
}