-1

This project involves writing a program to translate Morse Code into English and English into Morse Code. Your program shall prompt the user to specify the desired type of translation, input a string of Morse Code characters or English characters, then display the translated results.

When inputting Morse Code, separate each letter/digit with a single space, and delimit multiple words with a “|”. For example, - --- | -… . would be the Morse Code input for the sentence “to be”. Your program only needs to handle a single sentence and can ignore punctuation symbols.

When inputting English, separate each word with a blank space.

I'm getting a incomparable types: String and char error in the if ( Morse [ m ] == b.charAt ( m ) ) line. Any ideas on how to fix this? Thanks!

public class project {

public static void main ( String [] args ) {

char [] 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 = { ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" ,  ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." , "|" };
    String a = Input.getString ( "Please enter \"MC\" if you want to translate Morse Code into English, or \"Eng\" if you want to translate from English into Morse Code" );

    if ( a == "MC" )
    {
        String b = Input.getString ( "Please enter a sentence in Morse Code. Separate each letter/digit with a single space and delimit multiple words with a | ." );

        for ( int x = 0; x < Morse.length; x++ )
        {
            for ( int m = 0; m < b.length (); m++ )
            {
                if ( Morse [ m ] == b.charAt ( m ) )

                System.out.print ( English [ m ] + " " );

            }

        }

    }

    else if ( a == "Eng" )
    {
        String c = Input.getString ( "Please enter a sentence in English, and separate each word with a blank space." );

        c = c.toLowerCase ();

        for ( int x = 0; x < English.length; x++ )
        {
            for ( int y = 0; y < c.length (); y++ )
            {
                if ( English [ x ] == c.charAt ( y ) )

                System.out.print ( Morse [ x ] + " " );


            }

        }


    }

    else 
    {
        System.out.println ( "Invalid Input" );

    }

}

}

Devin
  • 7,690
  • 6
  • 39
  • 54
Daniel Du
  • 11
  • 1
  • 3
  • Read the error message again. What *is* it saying? How *does* it relate to the types of expressions actually used? How *do* a `String` and `char` differ, and why might they *not* be comparable? And then, *don't* use `==` for Strings anyway.. also, note that the Morse string (`".-"` for example) is always *more* than a single character; the input must be correctly divided along the separator. – user2864740 Aug 23 '14 at 01:29
  • `a == "MC"` MC is not a character. Second, when you compare Strings in Java you should use `equals()`. And third, use `String`s not `char`s. – Nir Alfasi Aug 23 '14 at 01:29
  • You are trying to compare a String type to a char type directly. Use one of the methods that the String class has for comparison. – OldProgrammer Aug 23 '14 at 01:30
  • @alfasin But `a == "MC"` isn't the type-problematic line ;-) – user2864740 Aug 23 '14 at 01:30
  • morse code and english are orthogonal: one is an encoding, the other a language. – ths Aug 23 '14 at 01:32
  • 2
    @user2864740 it's not *the* problematic line, it's one of many :) – Nir Alfasi Aug 23 '14 at 01:41

1 Answers1

2

This code that iterates though the morse code input isn't doing what you think its doing. You are iterating through the string pulling out individual characters despite asking the user to input multiple character sequences with a delimiter.

if ( a == "MC" )
    {
        String b = Input.getString ( "Please enter a sentence in Morse Code. Separate each letter/digit with a single space and delimit multiple words with a | ." );    

        for ( int x = 0; x < Morse.length; x++ )
        {
            for ( int m = 0; m < b.length (); m++ )
            {
                if ( Morse [ m ] == b.charAt ( m ) )    
                System.out.print ( English [ m ] + " " );    
            }    
        }    
    }

Change it so it actually loops though the separate morse code

if (a.equals("MC"))
    {
        String b = Input.getString ("Please enter a sentence in Morse Code. Separate each letter/digit with a single space and delimit multiple words with a | .");    

        String[] words = b.split("|");
        for (String word: words )
        {
            String[] characters = word.split(" ");
            for (String character: characters) 
            {
                if (character.isEmpty()) { continue; }
                for (int m = 0; m < b.length(); m++)
                {
                    if (character.equals(inputMorseCode[m])))    //This line is no longer failing because a string is being compared to a string
                        System.out.print(English[ m ]);    
                }    
            }
            System.out.print(" ");    
        }    
    }

Also in Java == compares if string are the same object reference. Use .equals() to see if the values are the same. See this question.

Community
  • 1
  • 1
ClassicThunder
  • 1,896
  • 16
  • 25