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!
The easier way to do it is to have an initializer that loads your characters into a hash map using the letter as the key. Then as your looping the characters in your input string your simply doing
at the beginning of you class do something like this:
private static HashMap<String, String> codes = new HashMap<String, String>();
static{
codes.put("a", ".-");
codes.put("b", "-...");
bla bla bla
}
then in your loop where you have
//convert character to morse code
getMorseChar = convert(currentChar);
you would have
getMorseChar = code.get(currentChar.toLowerCase());
no more nasty elseif statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With