I'm working on a translator that converts English to Morse code and the other way around. I wrote the code and am able to compile it but after enter the sentence which I want convert, the command prompt dash just goes to the next line and nothing comes out. Is it an problem with my code?

This is my code: ( not the most efficient as I'm still new to java so any tips would be appreciated as well)

import javax.swing.JOptionPane;

public class translator {
    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];
        char[] number = { 1 };

        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 = new String[36];

        morse[0] =  " .- ";
        morse[1] =  " -.. ";
        morse[2] =  " -.-. ";
        morse[3] =  " -.. ";
        morse[4] =  " . ";
        morse[5] =  " ..-. ";
        morse[6] =  " --. ";
        morse[7] =  " .... ";
        morse[8] =  " .. ";
        morse[9] =  " .--- ";
        morse[10] =  " -.- ";
        morse[11] =  " .-.. ";
        morse[12] =  " -- ";
        morse[13] =  " -." ;
        morse[14] =  " --- ";
        morse[15] =  " .--. ";
        morse[16] =  " --.- ";
        morse[17] =  " .-. ";
        morse[18] =  " ... ";
        morse[19] =  " - ";
        morse[20] =  " ..- ";
        morse[21] =  " ...- ";
        morse[22] =  " .-- ";
        morse[23] =  " -..- ";
        morse[24] =  " -.-- ";
        morse[25] =  " --.. ";
        morse[26] =  " .---- ";
        morse[27] =  " ..--- ";
        morse[28] =  " ...-- ";
        morse[29] =  " ....- ";
        morse[30] =  " ..... ";
        morse[31] =  " -.... ";
        morse[32] =  " --... ";
        morse[33] =  " ---.. ";
        morse[34] =  " ----. ";
        morse[35] =  " ----- ";

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

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

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

    public static void toEnglish( String phrase)
    {
        System.out.println( phrase );
    }
}
有帮助吗?

解决方案

Your code has an infinite loop in it.

Look at the variable J. You set the variable in a for/next loop, but then you test the variable in a while loop. Every time it finishes the while loop it tests to see if j < phrase.length(). But since J doesn't change until you LEAVE the while loop, it will never leave.

You don't need this test twice. Remove the while, let the for loop take care of it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top