Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8463279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:24:44+00:00 2026-06-10T14:24:44+00:00

I need help converting Morse Code to English. I have already written and tested

  • 0

I need help converting Morse Code to English. I have already written and tested the code for translating English to Morse. My main problem is having all the characters in the Morse code together before translating it to an English character if that makes sense.

Example: “.” translates to E and … translates to S, but I don’t want the translation to start until it reaches …

Few Rules
-Spaces are used to separate Morse Letters
-| is used as an delimiter to separate words
-Cant use an hashmap 🙁

Here is my code

import java.util.Arrays;
import javax.swing.JOptionPane;

public class test3
{
public static void main ( String [] args )
{

    String s1 = "Morse";

    // Decide whether Morse code or English
    String decide = JOptionPane.showInputDialog("Enter 'English' for Morse to English code translation and 'Morse' for English to Morse code translation. Pay attention to Caps.");

    // Enter String & decide whether to convert to Morse or English
    String phrase = JOptionPane.showInputDialog("Enter the words you wish to translate.");

    if ( decide.equals( s1 ))
        toMorse( phrase );

    else
        toEnglish( phrase );
}

// Translate to Morse
public static void toMorse( String preTranslation )
{

    String[] 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 = {".-","-...","-.-.","-..",".","..-.","--.","....","..", ".---",
            "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-", 
            "...-",".--","-..-","-.--","--..",".----","..---","...--","....-",".....","-....","--...","---..","----.","-----"};
    // Remove uppercase
    String preTrans = preTranslation.toLowerCase();

    // Deletes spaces
    String phraseWithDelimiter = preTrans.replace( " ", "|");

    String[] translation = new String[phraseWithDelimiter.length()];
    String[] delimiter = {"|"};

    // Translate 
    for ( int arrayVar = 0, transArrayVar = 0, subStringVarB = 0, subStringVarE = 1; transArrayVar < phraseWithDelimiter.length();)
    {
        if( phraseWithDelimiter.substring(subStringVarB, subStringVarE).equals( delimiter[0] ) )
        {
            translation[transArrayVar] = delimiter[0];
            transArrayVar++;
            subStringVarB++;
            subStringVarE++;
        }
        else if ( phraseWithDelimiter.substring(subStringVarB, subStringVarE).equals( english[arrayVar] ) )
            {
                translation[transArrayVar] = morse[arrayVar];
                transArrayVar++;
                subStringVarB++;
                subStringVarE++;
                arrayVar = 0;
            }
            else
            {
                arrayVar++;
                if ( arrayVar == 35 )
                {
                    arrayVar = 0;
                    subStringVarB++;
                    subStringVarE++;
                    transArrayVar++;
                }
            }
    }
String morseSeparator = new String( " " );
arrayToString ( translation, morseSeparator );

}


//Convert array to string and print translation
public static void arrayToString(String[] trans, String separator) 
{
    String result = "";
    if (trans.length > 0) 
    {
            result = trans[0];    // start with the first element
            for (int i = 1; i < trans.length; i++)
                result = result + separator + trans[i];
        }
    System.out.println( result );
}

    // unfinished
public static void toEnglish( String preTranslation)
{
    }

I was thinking about making a for statement and assigning every character of the Morse code to a new string until I reach a white space and then using that to test but I’m unsure about how to do that.

Any help is appreciated thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:24:45+00:00Added an answer on June 10, 2026 at 2:24 pm

    Create a regular expression containing all the Morse code sequences in order from longest to shortest.

     Pattern.compile("----\\.|-----|---\\.\\.|...")
    

    and then just match that repeatedly against a string to get the longest valid Morse code sequence.


    Alternatively, you can use a parser compiler like JavaCC.

    SKIP : { " " }
    TOKEN : { < A : "-." > }
    TOKEN : { < B : "-..." > }
    TOKEN : { < C : "-.-." > }
    ...
    
    String translate() :
    { StringBuilder sb; }
    {
      { sb = new StringBuilder(); }
    
      (
        letter(sb)
      )*
      <EOF>
    
      { return sb.toString(); }
    }
    
    void letter(StringBuilder sb) :
    {}
    {
      ( <A>
        { sb.append('a'); }
      | <B>
        { sb.append('b'); }
      | <C>
        { sb.append('c'); }
      ...
      )
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help converting a string to double with 7 decimals. I have a
I need help converting a DATE format to STRING format. I have a variable
I need help converting the following code snippet to use SPSiteDataQuery instead of SPQuery
I need help converting this code from C# to Java. public static ulong GetChecksum(byte[]
I need help converting eregi_replace to preg_replace (since in PHP5 it's depreciated): function makeClickableLinks($text)
I need help converting a VB.NET handles statement to C#. This is the VB
Converting to ODB.NET from System.Data.OracleClient and need help converting my connection string. Here is
I need some help converting decimal to hex with NO functions on python. I'm
Need help with a query that I wrote: I have three tables Company id
I need help in converting mp3 files to Apples Http Live Streaming protocol files.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.