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
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 { char [] chars = translate2.toCharArray(); if(translate2.equals(alpha[i])) { return morse[i]; } } return " "; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
