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]);
}
}
}
}
I believe you are trying to achieve something like this. You were on a good path, although you need to take a look at a few pointers I have for your code.
==
comparision sign.i
instead of the variable index
).for loop
at the end is necessary to compare every character from the input to every character of your english letters and numbers.String str
to concatenate the morse value. Then you simply have to print out it's value.Although I modified a bit your code, play around with with and see the different outputs it creates, this is the best way to learn from the code given.
Good luck with the learning
public static void main(String[] args){
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',
',', '.', '?' };
String[] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".---.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
"..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
"-----", "--..--", ".-.-.-", "..--.." };
//Scanner keyboard = new Scanner(System.in);
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(" ============================================ ");
//String userInput = keyboard.nextLine().toLowerCase();
String userInput = "TEST".toLowerCase();
char[] chars = userInput.toCharArray();
String str = "";
for (int i = 0; i < chars.length; i++){
for (int j = 0; j < english.length; j++){
if (english[j] == chars[i]){
str = str + morse[j] + " ";
}
}
}
System.out.println(str);
}