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 8399741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:23:05+00:00 2026-06-09T21:23:05+00:00

I am working on a English to Morse translator. When I input my sentence

  • 0

I am working on a English to Morse translator. When I input my sentence in English, I receive a translation filled with “null” rather than the corresponding Morse characters.

The result looks like this: “null|null|null|null|null”. | is what the delimiter for the Morse characters. How do I get rid of the null? Here is my code:

(Yes, this is homework.)

import javax.swing.JOptionPane;

public class test
{
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
    String phrasep = JOptionPane.showInputDialog("Enter the words you wish to translate.");

    if ( decide.equals( s1 ))
        toMorse( phrasep );
    else
        toEnglish( phrasep );
}

// Translate to Morse
public static void toMorse( String phrase1 )
{
    char[] english = new char[36];

    for (  int i = 65, j = 0; i < 91; i++, j++) {
        english[j] = (char)i;
    }

    english[26] = 1;
    english[27] = 2;
    english[28] = 3;
    english[29] = 4;
    english[30] = 5;
    english[31] = 6;
    english[32] = 7;
    english[33] = 8;
    english[34] = 9;
    english[35] = 0;

    String[] morse = {".-","-...","-.-.","-..",".","..-.","--.","....","..", ".---",
            "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-", 
            "...-",".--","-..-","-.--","--.."};

    //Replace spaces with |
    String phrase = phrase1.replace( "//s+", "|");

    String[] translation = new String[phrase1.length()];

    //Translate
    for ( int j = 0, t = 0, n = 1; j < phrase.length(); j++) {
        if ( phrase.substring(t, n ).equals ( english[j] ) ) {
            translation[t] = morse[j];
            t++;
            n++;
        }
    }

    String separatorp = new String( "|" );
    arrayToString ( translation, separatorp );
}

public static void toEnglish( String phrase) {
    System.out.println( phrase );
}

//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 );
}
}
  • 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-09T21:23:07+00:00Added an answer on June 9, 2026 at 9:23 pm

    There is significant confusion about how you’re writing this program. Let’s take your core functionality and consider it:

    public static String toMorse(String english) { ... }
    

    Unlike your implementation, note that it returns a String. This is because you’re giving it one string (the English phrase) and you want back another string (the Morse phrase). Always think first about what your data is, and write functions on that data. Don’t write the function and think about what your data is second.

    Consider now the essential part of the implementation of this function, your version here:

    //Translate
    for ( int j = 0, t = 0, n = 1; j < phrase.length(); j++) {
        if ( phrase.substring(t, n ).equals ( english[j] ) ) {
            translation[t] = morse[j];
            t++;
            n++;
        }
    }
    

    First, why are you instantiating three variables, which you fail to name (so what they mean is unclear to someone looking at it the first time) and which you increase in lockstep? The following does the same thing:

    //Translate
    for ( int j = 0, t=0; j < phrase.length(); j++) {
        if ( phrase.substring(t, (t+1) ).equals ( english[j] ) ) {
            translation[t] = morse[j];
            t++;
        }
    }
    

    But it’s still unclear what j and t do from the name. You’re really using them as an indices as you do a character-by-character conversion. If we look at the Java String API we see that you’re using substring() to get a particular character… except that your start and end points are j and j+1, which means you’re going to always get two characters. Two characters will never match a single character.

    The documentation linked above shows another option for retrieving a character.

    While we’re at it, please note:

    • If you’re following good programming style, your delimiter should be stored as a constant, not hard-coded into your function.
    • In one place you’re using replace() to replace instances of a particular thing with another. In a second place you’re exhaustively traversing an n-length list looking for a particular match. Use one method or the other. Using both is inappropriate here and confusing.
    • Your translation time is O(n*m), where n is the length of your phrase and m is the number of letters in your alphabet. Generally this will be considered to be O(n^2), which is bad. A Map is really what you want, because it’s O(1) access time, reducing your function to O(n), where n is the length of your phrase.
    • Your variables need to be clearly named so you clearly know what they do. The act of naming them will force you to keep straight in your head what they mean.
    • You should set up your dictionaries outside the function in which you utilize them. This way you can check them for correctness independent of your translation function.

    My final suggestion is that you step away from the code and try to write out, in plain English, the instructions to do this conversion. You seem hung up on details (such as pasting text to a swing front-end) that prevents you from solving the problem at hand: how to properly translate a phrase from one language to another.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a translator that converts English to Morse code and the other
I'm currently working on a program that converts japanese characters to english characters and
My Android application was working in English and I have to provide the users
I'm English but working for an American client, he wants the time displaying plus
I'm working on a english only C++ program for Windows where we were told
me working on search in a multilingual site (currently only english and turkish). As
I'm working on a php site that needs both Japanese and English. In most
I finished the English version of my application and now I am working on
The application that I'm working on supports 3 languages: English, French and German. How
how can i make java program working with multiple languages (frensh, english , arabic

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.