-
Notifications
You must be signed in to change notification settings - Fork 368
/
MorseCodeConverter.java
75 lines (66 loc) · 2.44 KB
/
MorseCodeConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.HashMap;
import java.util.Scanner;
public class MorseCodeConverter {
private static HashMap<Character, String> morseCodeMap;
static {
morseCodeMap = new HashMap<>();
morseCodeMap.put('A', ".-");
morseCodeMap.put('B', "-...");
morseCodeMap.put('C', "-.-.");
morseCodeMap.put('D', "-..");
morseCodeMap.put('E', ".");
morseCodeMap.put('F', "..-.");
morseCodeMap.put('G', "--.");
morseCodeMap.put('H', "....");
morseCodeMap.put('I', "..");
morseCodeMap.put('J', ".---");
morseCodeMap.put('K', "-.-");
morseCodeMap.put('L', ".-..");
morseCodeMap.put('M', "--");
morseCodeMap.put('N', "-.");
morseCodeMap.put('O', "---");
morseCodeMap.put('P', ".--.");
morseCodeMap.put('Q', "--.-");
morseCodeMap.put('R', ".-.");
morseCodeMap.put('S', "...");
morseCodeMap.put('T', "-");
morseCodeMap.put('U', "..-");
morseCodeMap.put('V', "...-");
morseCodeMap.put('W', ".--");
morseCodeMap.put('X', "-..-");
morseCodeMap.put('Y', "-.--");
morseCodeMap.put('Z', "--..");
morseCodeMap.put('0', "-----");
morseCodeMap.put('1', ".----");
morseCodeMap.put('2', "..---");
morseCodeMap.put('3', "...--");
morseCodeMap.put('4', "....-");
morseCodeMap.put('5', ".....");
morseCodeMap.put('6', "-....");
morseCodeMap.put('7', "--...");
morseCodeMap.put('8', "---..");
morseCodeMap.put('9', "----.");
morseCodeMap.put(' ', "/");
}
public static String convertToMorseCode(String text) {
StringBuilder morseCode = new StringBuilder();
text = text.toUpperCase();
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if (morseCodeMap.containsKey(ch)) {
morseCode.append(morseCodeMap.get(ch));
morseCode.append(" "); // Add space between characters
}
}
return morseCode.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a text string: ");
String text = scanner.nextLine();
String morseCode = convertToMorseCode(text);
System.out.println("Text: " + text);
System.out.println("Morse Code: " + morseCode);
scanner.close();
}
}