Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Everything is perfect for this code, except, that it is not displaying a respons

ID: 3873709 • Letter: E

Question

Everything is perfect for this code, except, that it is not displaying a response, once I type in a sentence. Please help me troubleshoot.

import java.util.Scanner;

import java.io.*;

public class TranslateCode {

   final String[] alpha = {"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", " "};

  

      final String[] morse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",

      ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",

      "...-" ,".--" ,"-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.","-----", "|"};

  

   public static void main (String args []) throws Exception {

      

      

       System.out.print("Type 1 for Morse to English translation. Input 2 for English to Morse translation: ");

       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

       int translate = Integer.parseInt(br.readLine());

  

      

      

       if (translate == 1)

       {

           Scanner Mor = new Scanner(System.in);

           System.out.print("- -.-- .--. .|.. -.|.-|... . -. - . -. -.-. .");

           String translate1 = Mor.nextLine();

           System.out.println(toEnglish(translate1));

          

       }

       if (translate == 2)

       {

           Scanner Eng= new Scanner(System.in);

           System.out.print("Type in a sentence: ");

           String translate2 = Eng.nextLine();

           System.out.println(toMorse(translate2));

          

       }

          

      

}

      

   public static String toEnglish(String translate1 )

   {

      

       final String[] alpha = {"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", " "};

      

          final String[] morse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",

          ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",

          "...-" ,".--" ,"-..-", "-.--", "--..", "|"};

      

      

           for(int i = 0; i < morse.length; ++i)

       {

       if(translate1.equals(morse[i]))

       return alpha[i];

       }

       return " ";

       }

  

   public static String toMorse(String translate2 )

   {

       final String[] alpha = {"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", " "};

      

          final String[] morse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",

          ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",

          "...-" ,".--" ,"-..-", "-.--", "--..", "|"};

         

          for (int i = 0; i<alpha.length; i++)

          {

             char [] chars = translate2.toCharArray();

             if(translate2.equals(alpha[i]))

                  

                     {

                 return morse[i];

                     }

          }

          return " ";

               }

  

  

  

   }

Explanation / Answer

Note: you have wrote program correct but its not comparing the characters correcty so that i chnaged that code ,BUT the logic what you have write for toEnglish method will work only for single morse letter ,I checked for toMorse code method it is working fine . I used String Array so the output will be tuples.


import java.util.Scanner;
import java.io.*;
import java.util.*;
public class TranslateCode {
final String[] alpha = {"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", " "};
  
final String[] morse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",
"...-" ,".--" ,"-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.","-----", "|"};
  

public static void main (String args []) throws Exception {
  
  
System.out.print("Type 1 for Morse to English translation. Input 2 for English to Morse translation: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int translate = Integer.parseInt(br.readLine());


if (translate == 1)
{
Scanner Mor = new Scanner(System.in);
System.out.print("- -.-- .--. .|.. -.|.-|... . -. - . -. -.-. .");
String translate1 = Mor.nextLine();
System.out.println(toEnglish(translate1));
  
}
if (translate == 2)
{
Scanner Eng= new Scanner(System.in);
System.out.print("Type in a sentence: ");
String translate2 = Eng.nextLine();
System.out.println(toMorse(translate2));
  
}
  
  
}
  

public static String toEnglish(String translate1 )

{

  

final String[] alpha = {"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", " "};

  

final String[] morse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",

".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",

"...-" ,".--" ,"-..-", "-.--", "--..", "|"};

for(int i = 0; i < morse.length; ++i)

{

if(translate1.equals(morse[i]))

return alpha[i];

}

return " ";

}

  
public static String toMorse(String translate2 )
{
final String[] alpha = {"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", " "};
  
final String[] morse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",
"...-" ,".--" ,"-..-", "-.--", "--..", "|"};
char [] chars = translate2.toCharArray();
String str[]= new String[translate2.length()] ;
String str1[]= new String[translate2.length()] ;
for(int i=0; i < chars.length; i++)
{
str[i]= Character.toString(chars[i]);
}
for(int j=0;j<translate2.length();j++)
{
for (int i = 0; i<alpha.length; i++)
{
  
if(str[j].equals(alpha[i]))
  
{

  
str1[j]=morse[i];
}
}

}
return Arrays.toString(str1);

}
   
}

output:

Type 1 for Morse to English translation. Input 2 for English to Morse translation: 2
Type in a sentence: pavan
[.--., .-, ...-, .-, -.]

Type 1 for Morse to English translation. Input 2 for English to Morse translation: 1
- -.-- .--. .|.. -.|.-|... . -. - . -. -.-. ....
s

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote