Can you please help me debug this code? Thank you! import java.util.Scanner; pub
ID: 3872894 • Letter: C
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", "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<morse.length; 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<alpha.length; i++)
{
char [] chars = translate2.toCharArray();
if(translate2.equals(alpha[i]))
{
return morse[i];
}
}
return " ";
}
}
Explanation / Answer
Hello there, i will elaborate complete debugging from line one and also tell you the possibilities to solve this problem.
* In the start, it has defined 2 string arrays namely alpha and morse to have both alpha and morse letter to conversion with size 27, because 26 are the letters in english and one for space.
* After that it defines main method, in which it creates the object(i.e input, in this case) of scanner class to take input from user as a choice of conversion whether it is english to morse or morse to english. it takes input in translate variable as 1 or 2 .
* After that it has one loop upto 37, which i think, is wrong, because we have 27 entries in our string array, and one more thing, why it is calling toAlpha() method, which is not even defined, and when it is already calling toEnglish() method from 'converting morse to english' case, so i think, this loop is irrelevant here.
* After that it has two checks. if(translate == 1) then take input as morse string and convert it into english string and if(translate == 2) then take input as english string and convert it into morse string. In both the cases it called seperate method for both named as toEnglish() and toMorse().
* Now comes the definition of method toEnglish(), in which, it again defined both the alpha and morse array, then it has 2 for loops upto morse array length, which i think is not necessary. one loop can work fine, there is no need to write 2 loops and one more thing to notice it passed variable name as translate1 but inside the loop it is performing comparison by variable dot(wrong variable name), so correct this issue. and one more thing it you are passing string of morse code then you have to tokenize it first into single tokens which can be comparable,in comparison, it checks if morse pattern matches with any pattern from morse array, it return the value from alpha array with the same index.
* Now comes the definition of method toMorse(), in which, it again defined both the alpha and morse array, then inside the loop, it converts our passed english string into character array so it can be easily comaprable.In comparison, it checks if english character matches with any pattern from alpha array, it return the value from morse array with the same index. Thats All
I hope this will be helpful for you, feel free to ask any queries and to give feedback and response.
Thank you