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

}

}

}

Hello, I am having some trouble completing my code. The English toMorse translation is perfect, but the Morse to English needs some work.

94 95 96 @ Javadoc Declaration Console TranslateCode [Java Application] /Library/Java/JavaVirtualMachines/jdk1.8.0_144 Would you like to enter a phrase in English or in Morse-code? EngLish Please enter your English phrase: Hello this is

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!