Question
Can you please help me debug this code? Thank you! import java.util.Scanner; public class TranslateCode { final String[] alpha = {a, b, c, d, e,
Can you please help me debug this code? Thank you!
import java.util.Scanner;
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", " "};
final String[] morse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",
"...-" ,".--" ,"-..-", "-.--", "--..", "|"};
public static void main (String args []) {
Scanner input = new Scanner(System.in);
System.out.print("Type 1 for Morse to English translation. Input 2 for English to Morse translation: ");
int translate = input.nextInt();
for (int j = 0; j < 37; ++j)
{
System.out.print(toAlpha(String dot));
}
if (translate == 1)
{
Scanner Mor = new Scanner(System.in);
System.out.print("- -.-- .--. .|.. -.|.-|... . -. - . -. -.-. .");
String translate1 = scan.nextLine();
System.out.println(toEnglish(translate1));
}
if (translate == 2)
{
Scanner Eng= new Scanner(System.in);
System.out.print("Type in a sentence: ");
String translate2 = scan.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 { for(int i = 0; i < morse.length; ++i) { if(dot.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
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