Search code examples
javamorse-code

Convert English into MorseCode


this java code is to convert english into morse code.

This program will terminate when user input a sentinel character $. Following each string entered, the program displays its morse code equivalent, leaving a blank space (ø) between characters and two øø between words.

EG. Hello World. Output should be ....ø.ø.-..ø.-..ø---øø.--ø---ø.-.ø.-..ø-..

My code has some problems. 1.I don't know where should i put $ to terminate the program while user inputs ø; 2.The output is correct but always ends with (ø), since System.out.print(code[variable] + "ø");

Here is my code:

public class Morse{
public static void main (String [] args)
{
    Scanner englishtomorse = new Scanner(System.in);
    System.out.println ("Please enter an original sentence in English.");
    english = englishtomorse.nextLine();
    String str = englishtoMorse(english);
    System.out.println(str);
}

public static String englishtoMorse(String english)
{
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String code[] =  {"._", "_...", "_._.", "_..",".",".._.","__.", "....",
        "..", ".___", "_._", "._..", "__","_ .", "___", ".__.", "__._", "._.",
        "...", "_", ".._", "..._", ".__", "_.._", "_.__", "__.."};
    english = english.toUpperCase();
    for(int xyz = 0; xyz < english.length(); xyz++)
    {
        char letter = english.charAt(xyz);
        if (letter == ' ')
        {
            System.out.print ("ø");
            continue;
        }
        for(int variable = 0; variable < alphabet.length(); variable++)
        {
            if(alphabet.charAt(variable) == letter)
            {
                System.out.print(code[variable] + "ø");
                break;
            }
        }
    }
    return " ";
}

Solution

  • Your code has some problems that you should be aware of.

    1. Take a look on Sun/Oracle's naming convention. It makes the code soooo much easier to read. camelCase it's a must.

    2. Scanner is considered a Resource on Java. So if you open it, you have to close it. Otherwise, you could have a resource leak problem. Closing a resource is just a simple method call but it shouldn't be ignored.

    Eg.:

    resourceVariableName.close().
    
    1. Where is the variable declaration from english on third line?

    english = englishtomorse.nextLine();


    First problem:

    The Scanner will continue to read until it's find an end of file condition as stated on Java SE API

    It's a common practice when using Scanner to process the data inside a loop and stop the next operation if it meets the end of file condition or found the "special" exit word.

    So you could refactor your code to consider the following situations:

    • Scanner found more than one line

    • User DID NOT type your reserved exit word ($)

    E.g.:

    Scanner myScanner = new Scanner(System.in);
    
    while(myScanner.hasNext() && !myScanner.equals("$")) {
        System.out.println(this.englishToMorse(myScanner.nextLine()));
    }
    
    myScanner.close();
    

    Second problem, you should review yours method logic. You state that you return a String but it always

    returns " ";

    Why should you return an empty String in any situation? A method signature it's important because you can predict what the method is capable to do and what it must return.

    Think about for a second, if you provide this class for a friend and he calls your method, how can he predict that you don't return the morse code as stated on your method signature return type?

    By the way, you could solve this problem in a better way without looping around two arrays. Take a look at Map data structure. It's a huge improvement on your overall solution.