1. Engineering
  2. Computer Science
  3. please read the question before you rush you answer ...

Question: please read the question before you rush you answer ...

Question details

Please read the question before you rush you answer , here is a screen shot of what's going on when I type in english hi it spits out Morse Code but when I enter the same Morse Code to translate to English it says "eeee"ee" trying to see what's going on .code and screen shot is below :

In a java program Need to find the purpose of this application have to create an application which is able to translate between back and forth between English – letters A-Z and numbers 0-9 – and Morse code – dots “.” and dashes “-“. The user will be prompted to enter something to translate. The user may enter either English or Morse code. The application will then analyze what the user entered to try and figure out if the user entered English or Morse code. If the analysis determines the user entered something in English, it will then translate it into Morse code. If the analysis determines the user entered something in Morse code, it will then translate it into English. At any time, the user may enter “-1” to exit the application.

HINT: Morse code is only dots “.” Dashes “-“ and spaces “ “. If the user input contains these 3 characters and only these 3 characters, it’s Morse code. Otherwise, assume it’s English.

program [Java Application] /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/bin/java (Jar hi hi Press any key to continue,-1 to exit Please input your message: k-kx--k--ok-x-o-ok-x-kk-k---o-ktok-

code:

import java.util.Scanner;

public class program

{

   public static String ToEng(String message) {

   String[] mCodes = { "*-*-*- ", "*-**-* ", "--**-- ", "**--** ", "-*--*- ", "-*--*-", "*----* ", "-*-*-- ",

   "-**-* ", "*---- ", "**--- ", "***-- ", "****- ", "***** ", "-**** ", "--*** ", "---** ", "----* ",

   "----- ", "--** ", "-*-- ", "-**- ", "*--- ", "*--* ", "***- ", "*-- ", "**-* ", "**- ", "-*-* ",

   "-*** ", "**** ", "*-** ", "*** ", "*-* ", "-** ", "--*- ", "-*- ", "--* ", "*- ", "-* ", "--- ", "** ",

   "-- ", "- ", "* " };

   String[] chars = { ".", """, ",", "?", "(", ")", "'", "!", "/",

   "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",

   "z", "y", "x", "j", "p", "v", "w", "f", "u", "c", "b", "h", "l", "s", "r", "d", "q", "k", "g", "a", "n", "o", "i", "m", "t", "e" };

   // splitting it into array

   String arr[] = message.split(" ");

   String temp = "";

   // iterating through the array

   for (String s : arr) {

   // getting index of morseCode

   int i = getIndex(s);

   // appening equalant char

   temp = temp + chars[i];

   }

   // printing code

   System.out.println(temp);

   return (temp);

   }

   private static int getIndex(String aS) {

   aS = aS + " ";

   String[] mCodes = { "*-*-*- ", "*-**-* ", "--**-- ", "**--** ", "-*--*- ", "-*--*-", "*----* ", "-*-*-- ",

   "-**-* ", "*---- ", "**--- ", "***-- ", "****- ", "***** ", "-**** ", "--*** ", "---** ", "----* ",

   "----- ", "--** ", "-*-- ", "-**- ", "*--- ", "*--* ", "***- ", "*-- ", "**-* ", "**- ", "-*-* ",

   "-*** ", "**** ", "*-** ", "*** ", "*-* ", "-** ", "--*- ", "-*- ", "--* ", "*- ", "-* ", "--- ", "** ",

   "-- ", "- ", "* " };

   int index = -1;

   for (String s : mCodes) {

   if (s.equalsIgnoreCase(aS))

   return ++index;

   index++;

   }

   return index;

   }

   public static String ToMorse(String Message) {

   String[] mcodes = { "*- ", "-*** ", "-*-* ", "-** ", "* ", "**-* ", "--* ", "**** ", "** ", "*--- ", "-*- ",

   "*-** ", "-- ", "-* ", "--- ", "*--* ", "--*- ", "*-* ", "*** ", "- ", "**- ", "***- ", "*-- ", "-**- ",

   "-*-- ", "--** ", /* numbers */ "*---- ", "**--- ", "***-- ", "****- ", "***** ", "-**** ", "--*** ",

   "---** ", "----* ", "----- ", /* Stop */ "*-*-*- ",

   /* quotation marks */ "*-**-* ", /* comma */ "--**-- ",

   /* question mark */ "**--** ", /* parentheses */"-*--*- ", "-*--*-", /* apostrophe */ "*----* ",

   /* exclamation mark */ "-*-*-- ", /* slash */ "-**-* " };

   String[] Chars = { "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[] caps = { "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" };

   for (int b = 0; b <= 44; b++) {

   Message = Message.replace(Chars[b], mcodes[b]);

   }

   for (int c = 0; c <= 25; c++) {

   Message = Message.replace(caps[c], mcodes[c]);

   }

   System.out.println(Message);

   return (Message);

   }

   public static void main(String[] args) {

   System.out.println("Welcome to Translator:");

   String ch="";

   // added loop to ask input from user repeatedly

   while(!ch.equals("-1")){

   System.out.println("Please input your message:");

   String msg;

   Scanner sc = new Scanner(System.in);

   msg = sc.nextLine();

   // if -1 exit

   if(msg.equals("-1"))

  

   break;

   System.out.println(msg + " ");

   if (msg.contains("*")) {

   ToEng(msg);

   }

   else {

   ToMorse(msg);

   }

   System.out.println("Press any key to continue,-1 to exit");

   ch=sc.next();

   }

   }

}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution