Answer to Question #57427 in Java | JSP | JSF for Eleni

Question #57427
Morse designed a coding system in which letters and other symbols are represented as coded sequences of short and long tones, traditionally called dots and dashes. The codes of the alphabet is easily found by searching "Morse Code". You can easily store these codes in a program by declaring an array with 26 elements and storing the sequence of characters corresponding to each letter in the appropriate array entry. Write a program that reads a string from the user and translates each letter in the string to its equivalent Morse code, using periods to represent dots and hyphens for dashes. Separate words in the output by calling println when there is a space in the input, but ignore punctuation.
1
Expert's answer
2016-01-22T08:00:51-0500
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class MorseCode {

public static void main(String[] args) throws IOException {
Map<String, String> alphabet = new HashMap<String, String>();
alphabet.put("a",".-");
alphabet.put("b","-...");
alphabet.put("c","-.-.");
alphabet.put("d","-..");
alphabet.put("e",".");
alphabet.put("f","..-.");
alphabet.put("g","--.");
alphabet.put("h","....");
alphabet.put("i","..");
alphabet.put("j",".---");
alphabet.put("k","-.-");
alphabet.put("l",".-..");
alphabet.put("m","--");
alphabet.put("n","-.");
alphabet.put("o","---");
alphabet.put("p",".--.");
alphabet.put("q","--.-");
alphabet.put("r",".-.");
alphabet.put("s","...");
alphabet.put("t","-");
alphabet.put("u","..-");
alphabet.put("v","...-");
alphabet.put("w",".--");
alphabet.put("x","-..-");
alphabet.put("y","-.--");
alphabet.put("z","--..");
alphabet.put(" ", " ");

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str = reader.readLine();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
String a = alphabet.get(String.valueOf(str.charAt(i)).toLowerCase());
if (!" ".equals(a)) {
if (null != a) {
stringBuilder.append(a);
}
} else {
System.out.println(stringBuilder.toString());
stringBuilder = new StringBuilder();
}
}
System.out.println(stringBuilder);
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
25.01.16, 14:19

Dear El this java. https://www.tutorialspoint.com/java/io/java_io_bufferedreader.htm can help you Best regards

El
22.01.16, 16:04

Is this code simple Java? Because I don't recognise the following: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map;

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS