3

I'm trying to learn about hashmaps and 2D arrays in Java. We have an assignment due to have a scanner that accepts a string and converts it to morse code. The code we're using is based of a method full of if statements, but I want to learn how I would do something like this using lists, hashmaps, or 2D arrays. My code is as follows:

import java.util.*;

public class MorseConversion 
{


public static void main(String[] args) 
{

    Scanner userInput = new Scanner(System.in); 
    System.out.println("Please enter a phrase to convert to morse code: ");
    String userString = userInput.nextLine();
    System.out.println("");
    System.out.println(stringConvert(userString));
}


public static String stringConvert(String userString)
{
    String currentChar;
    String getMorseChar;
    String convertedString = "";

    for (int i = 0; i < userString.length(); i++)
    {
        //Get character at i position
        currentChar = userString.charAt(i) + "";  

        //convert character to morse code
        getMorseChar = convert(currentChar);

        //seperate words with the | symbol
        if (getMorseChar.equals(" "))
        {
            convertedString = convertedString + "  |  ";
        } 

        else 
        {
            //concat the converted letter
            convertedString = convertedString + getMorseChar;

            //Add a space between each letter
            if (!getMorseChar.equals(" ")) 
            {
                convertedString = convertedString + " ";
            }   
        }           
    }

    return convertedString;

}
 public static String convert (String toEncode)  
    {  
        String morse = toEncode;  

        if (toEncode.equalsIgnoreCase("a"))  
            morse = ".-";  
        if (toEncode.equalsIgnoreCase("b"))  
            morse = "-...";  
        if (toEncode.equalsIgnoreCase("c"))  
            morse = "-.-.";  
        if (toEncode.equalsIgnoreCase("d"))  
            morse = "-..";  
        if (toEncode.equalsIgnoreCase("e"))  
            morse = ".";  
        if (toEncode.equalsIgnoreCase("f"))  
            morse = "..-.";  
        if (toEncode.equalsIgnoreCase("g"))  
            morse = "--.";  
        if (toEncode.equalsIgnoreCase("h"))  
            morse = "....";  
        if (toEncode.equalsIgnoreCase("i"))  
            morse = "..";  
        if (toEncode.equalsIgnoreCase("j"))  
            morse = ".---";  
        if (toEncode.equalsIgnoreCase("k"))  
            morse = "-.-";  
        if (toEncode.equalsIgnoreCase("l"))  
            morse = ".-..";  
        if (toEncode.equalsIgnoreCase("m"))  
            morse = "--";  
        if (toEncode.equalsIgnoreCase("n"))  
            morse = "-.";  
        if (toEncode.equalsIgnoreCase("o"))  
            morse = "---";  
        if (toEncode.equalsIgnoreCase("p"))  
            morse = ".--.";  
        if (toEncode.equalsIgnoreCase("q"))  
            morse = "--.-";  
        if (toEncode.equalsIgnoreCase("r"))  
            morse = ".-.";  
        if (toEncode.equalsIgnoreCase("s"))  
            morse = "...";  
        if (toEncode.equalsIgnoreCase("t"))  
            morse = "-";  
        if (toEncode.equalsIgnoreCase("u"))  
            morse = "..-";  
        if (toEncode.equalsIgnoreCase("v"))  
            morse = "...-";  
        if (toEncode.equalsIgnoreCase("w"))  
            morse = ".--";  
        if (toEncode.equalsIgnoreCase("x"))  
            morse = "-..-";  
        if (toEncode.equalsIgnoreCase("y"))  
            morse = "-.--";  
        if (toEncode.equalsIgnoreCase("z"))  
            morse = "--..";  
        if (toEncode.equalsIgnoreCase("0"))  
            morse = "-----";  
        if (toEncode.equalsIgnoreCase("1"))  
            morse = ".----";  
        if (toEncode.equalsIgnoreCase("2"))  
            morse = "..---";  
        if (toEncode.equalsIgnoreCase("3"))  
            morse = "...--";  
        if (toEncode.equalsIgnoreCase("4"))  
            morse = "....-";  
        if (toEncode.equalsIgnoreCase("5"))  
            morse = ".....";  
        if (toEncode.equalsIgnoreCase("6"))  
            morse = "-....";  
        if (toEncode.equalsIgnoreCase("7"))  
            morse = "--...";  
        if (toEncode.equalsIgnoreCase("8"))  
            morse = "---..";  
        if (toEncode.equalsIgnoreCase("9"))  
            morse = "----.";  
        if (toEncode.equalsIgnoreCase("."))  
            morse = ".-.-";  
        if (toEncode.equalsIgnoreCase(","))  
            morse = "--..--";  
        if (toEncode.equalsIgnoreCase("?"))  
            morse = "..--..";  

        return morse;  
    }  

 }

I'm doing this strictly out of curiosity. I have had it pounded into my head that redundancy like this is a huge no-no. Thanks in advance!

4

2 に答える 2

6

これを行う簡単な方法は、文字をキーとして使用して文字をハッシュ マップにロードするイニシャライザを用意することです。次に、入力文字列の文字をループするだけで、

クラスの最初に、次のようにします。

private static HashMap<String, String> codes = new HashMap<String, String>();
    static{
        codes.put("a", ".-");
        codes.put("b", "-...");
        bla bla bla
    }

次に、あなたが持っているループで

//convert character to morse code
        getMorseChar = convert(currentChar);

あなたが持っているだろう

getMorseChar = code.get(currentChar.toLowerCase());

もう厄介なelseifステートメントはありません。

于 2013-02-27T00:16:54.487 に答える