Search code examples
javaarraysmorse-code

English - Morse Translator


Translating from Morse to English works like a charm, however translating a phrase or sentence (multiple words separated by a space) from English to Morse yields only the first word translated to Morse. For example, if I were to enter 'Hello World', the translator would only return

'.... . .-.. .-.. --- '.

What do I need to change in order to obtain the full sentence from the translator? I have indicated where I think the problem is, and note that the translator ignores punctuation. TIA

import java.util.Scanner;

public class JavaProgram
{
    //made them static so they can be accessed from static functions
    private final static char[] ABC = {'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', ' '};

    private final static String[] MORSE = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
        ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",  ".-.", "...", "-", "..-",
        "...-" ,".--" ,"-..-", "-.--", "--..", "|"};

    public static void main(String [] args)
    {     
        Scanner scan = new Scanner( System.in );
        System.out.print("If you want to convert from Morse to English, type 'M', if you would like to convert from English to Morse, type 'E': ");
        String answer = scan.nextLine();
        if( answer.equals("E"))
        {
            Scanner scan2 = new Scanner(System.in);
            System.out.print("Enter a word or phrase in English: ");
            String englishInput = scan.next();
            System.out.println(toMorse(englishInput));
            scan2.close();
        }
        if (answer.equals("M"))
        {           
            Scanner scan3 = new Scanner(System.in);
            System.out.print("Enter a word or phrase in Morse: ");
            String morseInput = scan.nextLine();
            System.out.println(getABC(morseInput));
            scan3.close();
                }
        scan.close();
        }


    private static String toMorse(String ABCString)
    {
        String morseString = "";
        for (char c : ABCString.toLowerCase().toCharArray()) 
        {
            morseString += charToMorse(c) + " ";
        }
        return morseString;
    }

    //**I am fairly certain the problem is in this method.**
    private static String charToMorse(char c)
    {
        for(int i = 0; i < ABC.length; ++i)
        { 
            if (c == ABC[i])
            {
                return MORSE[i];
            }
        }
        return String.valueOf(c); 
    }


    private static String getABC(String morseString)
    {
        String ABCString = "";
        for (String s : morseString.split("\\s")) 
        {
            ABCString += characterToABC(s);
        }
        return ABCString;
    }


    private static String characterToABC(String s)
    {
        for (int i = 0; i < MORSE.length; ++i) 
        {
            if (s.equals(MORSE[i])) 
            {
                return String.valueOf(ABC[i]); 
            }
        }
        return s; 
    }
}

Solution

  • I believe, based on http://www.tutorialspoint.com/java/util/scanner_next.htm (the definition of java.util.Scanner.next), that your String englishInput = scan.next();line in this section:

    if( answer.equals("E"))
    {
        Scanner scan2 = new Scanner(System.in);
        System.out.print("Enter a word or phrase in English: ");
        String englishInput = scan.next();
        System.out.println(toMorse(englishInput));
        scan2.close();
    }
    

    only reads the first token, which is the first word, since single spaces ARE whitespace, as opposed to your nextLine() which correctly takes in an entire sentence.