0

I am still very new to Java and have been trying to get a morse code translator to work. I overcame the first problem with various errors, but now the program compiles but will not print the results of the translation. Any help would be appreciated.

import java.util.Scanner;

public class MorseCode
{
    public static void main(String[] args)
    {
        Scanner Input = new Scanner(System.in);

        System.out.println("To convert English to Morse Code, type M. To convert Morse Code to English, type E.");

        String cType = Input.nextLine();

        String type = cType.toLowerCase();

        if(type == "m")
        {
            String eng;
            System.out.println("Please enter the English text to be translated.");
            eng = Input.nextLine();
            EToM(eng);
        }
        else
        {
            String morse;
            System.out.println("Please enter the Morse code text to be translated, with multiple words seperated by a |.");
            morse = Input.nextLine();
            MToE(morse);
        }
    }
    public static void EToM(String eng)
    {
        String EToMList[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".--", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "----.", "-----", "|"};

        String alphabet = "abcdefghijklmnopqrstuvwxyz123456789 ";
        String translation[] = new String[eng.length()];

        for(int x = 0; x < eng.length(); x++)
        {
            for(int y = 0; y < alphabet.length(); y++)
            {
                if(eng.charAt(x) == alphabet.charAt(y))
                {
                    translation[x] = EToMList[y];
                    System.out.println("test");
                }
            }
        }

        System.out.println("Your translated message is:");

        for(int z = 0; z < eng.length(); z++)
        {
            System.out.println(translation[z]); 
        }
    }

    public static void MToE(String morse)
    {

    }
}       
3
  • try printing out something after you print out the contents of your array, and see how many iterations of your loop are running Commented Aug 16, 2013 at 15:19
  • 1
    and use equals() instead of == for String/Object comparison Commented Aug 16, 2013 at 15:20
  • Although your method is grossly inefficient in terms of coding, it compiles and runs fine (demo link). Commented Aug 16, 2013 at 15:23

4 Answers 4

3

Your problem is

if(type == "m")

use

if("m".equals(type))

Your if-else will go to else because you are comparing String references and not String values. The else calls the MToE method which is empty. Read this: How Do I compare Strings in Java

Sign up to request clarification or add additional context in comments.

6 Comments

It looks to me like everything that the OP expects to happen before the printing of the translation is happening. If type=="m" was the issue, than it would never even print the "test" lines that the OP has
@SamIam I haven't seen OP state that test was printed.
Why would the OP tell you that the problem was with the results if there's evidence of failure before results?
@SamIam Because OP is still very new to Java. OP mus think there are results, but there actually aren't.
That's why i didn't actually down-vote; it's foolish to assume that the OP didn't miss any obvious symptoms, but I'm not up-voting either, at least not until the OP verifies that it is the issue.
|
0

When checking strings for equality in Java, always use the equals method on the String class. Changing the following:

if(type == "m")

to

if(type.equals("m"))

makes the English to Morse code translation output.

I made this modification and ran it successfully just now.

Comments

0

use This

  if(type.equalsIgnoreCase("m")
  {

  }

Comments

-1

You can use the deepToString method from Java to print out your array.

Documentation Link: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html#deepToString(java.lang.Object[])

Here is a code sample:

System.out.println("Your translated message is:" + java.util.Arrays.deepToString(translation));    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.