For my Intro to Java class I'm supposed to create a Morse code translator that can convert both from English to Morse and from Morse to English. My code works for converting English to Morse, but can't seem to manage to go the other way, from Morse Code to English. Here is the prompt:
This project involves writing a program to translate Morse Code into English and English into Morse Code. Your program shall prompt the user to specify the desired type of translation, input a string of Morse Code characters or English characters, then display the translated results. When inputting Morse Code, separate each letter/digit with a single space, and delimit multiple words with a “|”. For example, - --- | -… . would be the Morse Code input for the sentence “to be”. Your program only needs to handle a single sentence and can ignore punctuation symbols. When inputting English, separate each word with a blank space.
I am wondering what's wrong with my current "Morse code" code. Please help if you understand what's going wrong, I've spent hours trying to figure this thing out and I've got to this before midnight Pacific Time on 8/26/2015. Thanks! Here is my code:
//Justin Buckley
//8.26.2015
import java.util.Scanner;
public class MorseCodeProject1 {
public static void main (String[] args)
{
char [] English = { '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', '0' };
String [] Morse = { ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" , ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." , "|" };
Scanner input = new Scanner (System.in);
System.out.println( "Please enter \"MC\" if you want to translate Morse Code into English, or \"Eng\" if you want to translate from English into Morse Code" );
String a = input.nextLine();
if ( a.equalsIgnoreCase("MC"))
{
System.out.println( "Please enter a sentence in Morse Code. Separate each letter/digit with a single space and delimit multiple words with a | ." );
String b = input.nextLine();
String[] words = b.split("|");
for (String word: words )
{
String[] characters = word.split(" ");
for (String character: characters)
{
if (character.isEmpty()) { continue; }
for (int m = 0; m < Morse.length; m++)
{
if (character.equals(Morse[m]))
System.out.print(English[ m ]);
}
}
System.out.print(" ");
}
}
else if ( a.contains("Eng" ))
{
System.out.println("Please enter a sentence in English, and separate each word with a blank space.");
String c = input.nextLine();
c = c.toLowerCase ();
for ( int x = 0; x < English.length; x++ )
{
for ( int y = 0; y < c.length(); y++ )
{
if ( English [ x ] == c.charAt ( y ) )
System.out.print ( Morse [ x ] + " " );
}
}
}
else
{
System.out.println ( "Invalid Input" );
}
}
}
here this code will work for you, you had your english to morse code wrong too i fixed that too
Following on from my comment: You really really need to do your homework before asking this sort of question.
All the answers can be found in the Java tutorial: https://docs.oracle.com/javase/tutorial/java/data/strings.html
More specifically you want to know how to Manipulating a string: https://docs.oracle.com/javase/tutorial/java/data/manipstrings.html
In keeping with the tutorials here is a basic example showing how you can achieve what you want: