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]);
}
}
}
}