2

I have been trying to create a Morse code translator and I have run into a few problems. It compiles, but when I try to run it it only asks the first question of which way you would like to translate, and not what you would like to translate. I know my first function is very inefficient. I also use Input.getString which I do not think is standard but it basically allows you input a string.

This is my code:

public class MorseCodeJavaProgram {
    public static void morse( String s3 ){
        int letters [ ] = new int [ 26 ];
        for ( int num = 0; num < s3.length(); num++ ){
                switch ( s3.charAt( num ) ){
                    case 'a':
                        System.out.print( ".- ");
                        break;
                    case 'b':
                        System.out.print( "-… ");
                        break;
                    case 'c':
                        System.out.print( "-.-. ");
                        break;
                    case 'd':
                        System.out.print( "-.. ");
                        break;
                    case 'e':
                        System.out.print( ". ");
                        break;
                    case 'f':
                        System.out.print( "..-. ");
                        break;
                    case 'g':
                        System.out.print( "--. ");
                        break;
                    case 'h':
                        System.out.print( "…. ");
                        break;
                    case 'i':
                        System.out.print( ".. ");
                        break;
                    case 'j':
                        System.out.print( ".--- ");
                        break;
                    case 'k':
                        System.out.print( "-.- ");
                        break;
                    case 'l':
                        System.out.print( ".-.. ");
                        break;
                    case 'm':
                        System.out.print( "-- ");
                        break;
                    case 'n':
                        System.out.print( "-. ");
                        break;
                    case 'o':
                        System.out.print( "--- ");
                        break;
                    case 'p':
                        System.out.print( ".--. ");
                        break;
                    case 'q':
                        System.out.print( "--.- ");
                        break;  
                    case 'r':
                        System.out.print( ".-. ");
                        break;  
                    case 's':
                        System.out.print( "... ");
                        break;
                    case 't':
                        System.out.print( "- ");
                        break;  
                    case 'u':
                        System.out.print( "..- ");
                        break;  
                    case 'v':
                        System.out.print( "...- ");
                        break;
                    case 'w':
                        System.out.print( ".-- ");
                        break;
                    case 'x':
                        System.out.print( "-..- ");
                        break;
                    case 'y':
                        System.out.print( "-.-- ");
                        break;
                    case 'z':
                        System.out.print( "--.. ");
                        break;
                    case ' ':
                        System.out.print( " | ");
                        break;
                }

            }
    }
    public static void toEnglish( String s1 ){
    String english [ ] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "x", "y", "z", " " };
    String morse [ ] = { ".- ", "-... ", "-.-. ", "-.. ", ". ", "..-. ", "--. ", "…. ", ".. ", ".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ", "--.- ", ".-. ", "... ", "- ", "..- ", "...- ", ".-- ", "-..- ", "-.-- ", "--.. ", "| " };

        for ( int num = 0; num < s1.length(); num++ ){
            if ( s1.charAt ( num ) == ' '){
                for ( int num2 = num; num2 < s1.length(); num2++ ){
                    if ( s1.charAt ( num2++ ) == ' '){
                        for ( int num3 = 0; num < 26; num3++ ){
                            if ( s1.substring( num++, num2 + 2 ) == ( morse [ num3 ] )){
                                System.out.print( english [ num3 ] );
                            }
                        }
                    }

                }


            }
        }
    }

    public static void main( String [] args ){
        String s2 = Input.getString( "To Morse or From Morse" );
        if ( s2 == "From Morse" ){
            String s1 = Input.getString( "Please type a phrase in English" );
            toEnglish( " " + s1 + " " );
        }
        if ( s2 == "To Morse" ){
            String s3 = Input.getString( "Please type a phrase in Morse Code" );
            morse( s3 );
        }
    }
    }
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
user1690013
  • 31
  • 1
  • 3

2 Answers2

4

You are comparing strings using ==. In Java, == compares references, not content.

Change them to use .equals(), like so:

if ( s2 == "From Morse" ){

Should be:

if ( s2.equals( "From Morse" ) ){

And of course, this should apply to your other string comparison(s) as well. (Your char comparisons are fine as-is.)

Community
  • 1
  • 1
Cat
  • 66,919
  • 24
  • 133
  • 141
  • thank you but now I am getting a error about accessing the -1 index of an array – user1690013 Oct 26 '12 at 00:30
  • You may want to close this thread (the checkmark to the side of the answer) and open a new one. I'd post only the `toEnglish` method, and post the full error output. Your loops definitely need to be cleaned up a bit, but more than I can possibly do through comments. – Cat Oct 26 '12 at 02:47
1

You are comparing string value with == operator. But == operator only check object reference are equal or not.

 if ( s2 == "From Morse" ){
            String s1 = Input.getString( "Please type a phrase in English" );
            toEnglish( " " + s1 + " " );
        }

instead you try

 if ("From Morse".equals(s2) ){
                String s1 = Input.getString( "Please type a phrase in English" );
                toEnglish( " " + s1 + " " );
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37