Question
Hello, I am having some trouble completing my code. The English to Morse translation is perfect, but the Morse to English needs some work. I
Hello, I am having some trouble completing my code. The English to Morse translation is perfect, but the Morse to English needs some work. I am trying to separate letters using a single space and to seperate words by "|".
import java.util.Scanner;
/*
* this code works for one sentence using numbers 1-9 and alphabet letters and space.
* When inputing Morse code, put "|" between each letter.
* spalmer
*/
public class TranslateCode {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
//input = string names
String userResponse = "";
String english = "English";
String morse = "Morse-Code";
String morseChars = "";
String morseMultiWords = "";
String morseWords = "";
String phrase = "";
//create strings for languages
final String[] englishArray = {"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", " ",};
final String[] morseArray = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", " ",
".----", "..---", "...--", "....-", ".....", "-....", "--...",
"---..", "----.", "-----", "|"};
System.out.print(" Would you like to enter a phrase in English or in Morse-code? ");
userResponse = input.next();
//input phrase English
if(userResponse.equalsIgnoreCase(english))
{
System.out.print(" Please enter your English phrase: ");
input.nextLine();
phrase = input.nextLine();
//use loops to convert English loop to morse code
phrase = phrase.toUpperCase();
for(int count = 0; count
{
for(int index = 0; index
{
if(phrase.substring(count, (count+1)).equals(englishArray[index]))
System.out.print(morseArray[index] + " ");
}
}
}
//input phrase morse
else if(userResponse.equalsIgnoreCase(morse))
{
System.out.print(" Please enter your Morse-code phrase: ");
input.nextLine();
phrase = input.nextLine();
//this is where my program is not working correctly
String[] morseMultipleWords = phrase.split(" ");
for(int i = 0; i
{
morseMultiWords = morseMultipleWords[i];
String[] morseCharacters = morseMultiWords.split(" ");
for(int j = 0; j
{
morseChars += morseCharacters[j];
for(int index = 0; index
{
if(morseChars.equals(morseArray[index]))
morseWords += englishArray[index];
}
morseChars = " ";
}
morseWords += "";
morseMultiWords = "";
}
System.out.println(morseWords);
}
}
}
94 95 96 @ Javadoc Declaration Console
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started