Search code examples
javaarraysfor-loopwhile-loopnested-loops

Morse Code Translator - Can't seem to convert from morse-to-english accurately


So I have an assignment due and I have spent ages trying to figure out a specific part of the problem, but keep coming up with nothing. I finally 'gave up' and decided to come here for a little assistance.

The question reads as follows:

"Write a program that reads an English language phrase and encodes the phrase into Morse code or reads in a phrase in Morse code and converts the phrase into English. Use one blank between each Morse-code letter and three blanks between each Morse-code word."

The bit I am having trouble with is the bolded sentence above. I've got the rest of the program working from English-to-Morse, but in reverse it just messes up.

Here's what I know is happening:

The Morse code for the letter 'e' is '.' and 't' is '-', and the letter 'a' is '.-', in my current code, if you were to input '.-' in the morse-to-english area, it's going to return the translation as 'et', which isn't right. It's reading each individual dot and dash, when what it needs to be doing is reading the entire block of dots and dashes and trying to find it's equivalent. I figured the one-blank and three-blanks in the question is where I am messing up, but I just can't work out how to implement this. I figured it means that everything prior to a single blank space will be read in it's entirety, and then the three-blank space will simply translate to a single-blank to ensure the English translation is readable as a sentence.

Here's my current code. I'm a new Java programming student, and have been told I must answer this problem using arrays. Thanks:

import java.util.*;

public class MorseCode {

public static void main(String[] args)
{
      Scanner input = new Scanner(System.in);

      String userResponse = "";
      String english = "English";
      String morse = "Morse-Code";
      String phrase = "";
      String answer = "";
      int loop = 0;

      final String[] englishArray = {"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"};

      final String[] morseArray = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
                             ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
                             "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", " ", 
                             ".----", "..---", "...--", "....-", ".....", "-....", "--...", 
                             "---..", "----.", "-----"};

      while(loop == 0)
      {
          System.out.print("\nWould you like to enter a phrase in English or in Morse-code? ");
          userResponse = input.next();  

          while(!(userResponse.equalsIgnoreCase(english) || userResponse.equalsIgnoreCase(morse)))
          {
              System.out.println("\nInvalid response. \nPlease enter 'English' or 'Morse-code'.\n");
              System.out.print("Would you like to enter a phrase in English or in Morse-code? ");
              userResponse = input.next();
          }

          if(userResponse.equalsIgnoreCase(english))
          {
              System.out.print("\nPlease enter your English phrase: ");
              input.nextLine();
              phrase = input.nextLine();    

              System.out.println("\nYou entered: " + phrase);
              phrase = phrase.toUpperCase();
              System.out.print("In morse code, this is: ");

              for(int count = 0; count < phrase.length(); count++ )
              { 
                  for(int index = 0; index < englishArray.length; index++) 
                  { 
                      if(phrase.substring(count, (count+1)).equals(englishArray[index]))
                          System.out.print(morseArray[index] + " "); 
                  } 
              } 
          }
          else if(userResponse.equalsIgnoreCase(morse))
          {
              System.out.print("\nPlease enter your Morse-code phrase: ");
              input.nextLine();
              phrase = input.nextLine();    

              System.out.println("\nYou entered: " + phrase);
              System.out.print("In English, this is: ");

              for(int count = 0; count < phrase.length(); count++ )
              { 
                  for(int index = 0; index < morseArray.length; index++) 
                  { 
                      if(phrase.substring(count, (count+1)).equals(morseArray[index])) 
                          System.out.print(englishArray[index]); 
                  } 
              } 
          }
          loop++;

          System.out.print("\n\nWould you like to enter another phrase? (Y or N): ");
          answer = input.next();

          while(!(answer.equalsIgnoreCase("Y") || answer.equalsIgnoreCase("N")))
            {
                System.out.print("\nIncorrect input. Please enter either 'Y' or 'N'.");
                System.out.print("Would you like to create 20 sentences? (Y or N): ");
                answer = input.next();  
            }
          if(answer.equalsIgnoreCase("Y"))
          {
              loop = 0;
          }
          else
              {
                System.out.println("Program ended.");
                input.close();
              }

      }
}

}


Solution

  • I solved my problem, using the advice given above. I ended up using String.split().

    Thank you for your help! :)

    import java.util.*;
    
    public class MorseCode {
    
    public static void main(String[] args)
    {
    
          Scanner input = new Scanner(System.in);
    
          String userResponse = "";
          String english = "English";
          String morse = "Morse-Code";
          String morseChars = "";
          String morseMultiWords = "";
          String morseWords = "";
          String phrase = "";
          String answer = "";
          int loop = 0;
    
          final String[] englishArray = {"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"};
    
          final String[] morseArray = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
                                 ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
                                 "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", " ", 
                                 ".----", "..---", "...--", "....-", ".....", "-....", "--...", 
                                 "---..", "----.", "-----"};
    
          while(loop == 0)
          {   
              System.out.print("\nWould you like to enter a phrase in English or in Morse-code? ");
              userResponse = input.next();  
    
    
              while(!(userResponse.equalsIgnoreCase(english) || userResponse.equalsIgnoreCase(morse)))
              {
                  System.out.println("\nInvalid response. \nPlease enter 'English' or 'Morse-code'.\n");
                  System.out.print("Would you like to enter a phrase in English or in Morse-code? ");
                  userResponse = input.next();
              }
              if(userResponse.equalsIgnoreCase(english))
              {
                  System.out.print("\nPlease enter your English phrase: ");
                  input.nextLine();
                  phrase = input.nextLine();    
    
                  System.out.println("\nYou entered: " + phrase.toUpperCase());
                  phrase = phrase.toUpperCase();
                  System.out.print("In morse code, this is: ");
    
                  for(int count = 0; count < phrase.length(); count++ )
                  { 
                      for(int index = 0; index < englishArray.length; index++) 
                      { 
                          if(phrase.substring(count, (count+1)).equals(englishArray[index]))
                              System.out.print(morseArray[index] + " "); 
                      } 
                  } 
              }
    
              else if(userResponse.equalsIgnoreCase(morse))
              {
                  System.out.print("\nPlease enter your Morse-code phrase: ");
                  input.nextLine();
                  phrase = input.nextLine();
    
                   String[] morseMultipleWords = phrase.split("   ");
    
                  System.out.println("\nYou entered: " + phrase);
                  System.out.print("In English, this is: ");
    
                  for(int i = 0; i < morseMultipleWords.length; i++)
                  {
                      morseMultiWords = morseMultipleWords[i];
    
                      String[] morseCharacters = morseMultiWords.split(" ");
    
                      for(int j = 0; j < morseCharacters.length; j++)
                      {
                          morseChars += morseCharacters[j];
    
    
                          for(int index = 0; index < morseArray.length; index++) 
                          { 
                              if(morseChars.equals(morseArray[index])) 
                                  morseWords += englishArray[index];
                          }
                          morseChars = "";
                      }
                      morseWords += " "; 
                      morseMultiWords = "";  
                  }
                  System.out.println(morseWords); 
              }
              loop++;
    
              System.out.print("\n\nWould you like to enter another phrase? (Y or N): ");
              answer = input.next();
    
              while(!(answer.equalsIgnoreCase("Y") || answer.equalsIgnoreCase("N")))
                {
                    System.out.print("\nIncorrect input. Please enter either 'Y' or 'N'.");
                    System.out.print("Would you like to create 20 sentences? (Y or N): ");
                    answer = input.next();  
                }
              if(answer.equalsIgnoreCase("Y"))
              {
                  morseWords = "";
                  loop = 0;
              }
              else
                  {
                    System.out.println("Program ended.");
                    input.close();
                  }
    
          }
    }
    

    }