Hi, I have this program almost completed. However, it is not allowing me to ente
ID: 3873307 • Letter: H
Question
Hi, I have this program almost completed. However, it is not allowing me to enter Text, once a reader selects for version of translation. Can you please help me be able to run this program?
import java.util.Scanner;
public class Assignment5 {
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 []) {
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();
if (translate == 1)
{
Scanner Mor = new Scanner(System.in);
System.out.print("- -.-- .--. .|.. -.|.-|... . -. - . -. -.-. .");
String translate1 = input.nextLine();
System.out.println(toEnglish(translate1));
}
if (translate == 2)
{
Scanner Eng= new Scanner(System.in);
System.out.print("Type in a sentence: ");
String translate2 = input.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
public static void main (String args []) throws Exception {
// i made the following changes
//the previous code was not executed so i made this
//now you can theck it
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());
//int translate = name.toInt();
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));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.