Skip to content

Commit fd23ff8

Browse files
committed
Initial Commit
0 parents  commit fd23ff8

6 files changed

Lines changed: 125 additions & 0 deletions

File tree

.idea/discord.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MorseCodeTranslator.iml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
8+
</content>
9+
<orderEntry type="inheritedJdk" />
10+
<orderEntry type="sourceFolder" forTests="false" />
11+
</component>
12+
</module>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package me.moruto.morsecodetranslator;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Scanner;
6+
7+
public class Main {
8+
private static final Map<String, String> dictionary = new HashMap<String, String>() {{
9+
put("A", ".-");
10+
put("B", "-...");
11+
put("C", "-.-.");
12+
put("D", "-..");
13+
put("E", ".");
14+
put("F", "..-.");
15+
put("G", "--.");
16+
put("H", "....");
17+
put("I", "..");
18+
put("J", ".---");
19+
put("K", "-.-");
20+
put("L", ".-..");
21+
put("M", "--");
22+
put("N", "-.");
23+
put("O", "---");
24+
put("P", ".--.");
25+
put("Q", "--.-");
26+
put("R", ".-.");
27+
put("S", "...");
28+
put("T", "-");
29+
put("U", "..-");
30+
put("V", "...-");
31+
put("W", ".--");
32+
put("X", "-..-");
33+
put("Y", "-.--");
34+
put("Z", "--..");
35+
// Add spaces for readability in translation output
36+
put(" ", " / ");
37+
}};
38+
39+
public static void main(String[] args) {
40+
Scanner scanner = new Scanner(System.in);
41+
System.out.println("Choose mode:");
42+
System.out.println("1: English to Morse");
43+
System.out.println("2: Morse to English");
44+
45+
String choice = scanner.nextLine().trim();
46+
47+
if (!choice.equals("1") && !choice.equals("2")) {
48+
throw new IllegalArgumentException("Invalid choice. Please enter 1 or 2.");
49+
}
50+
51+
System.out.println("Enter your text: ");
52+
String input = scanner.nextLine().trim().toUpperCase();
53+
54+
if (choice.equals("1")) {
55+
System.out.println("Translated: " + translateToMorse(input));
56+
return;
57+
}
58+
59+
System.out.println("Translated: " + translateToEnglish(input));
60+
}
61+
62+
private static String translateToMorse(String input) {
63+
StringBuilder result = new StringBuilder();
64+
for (char character : input.toCharArray()) {
65+
String morseCode = dictionary.getOrDefault(String.valueOf(character), "?");
66+
result.append(morseCode).append(" ");
67+
}
68+
return result.toString().trim();
69+
}
70+
71+
private static String translateToEnglish(String morseInput) {
72+
StringBuilder result = new StringBuilder();
73+
String[] morseWords = morseInput.split(" / ");
74+
for (String morseWord : morseWords) {
75+
for (String morseChar : morseWord.split(" ")) {
76+
result.append(dictionary.entrySet().stream()
77+
.filter(entry -> entry.getValue().equals(morseChar))
78+
.map(Map.Entry::getKey)
79+
.findFirst()
80+
.orElse("?"));
81+
}
82+
result.append(" ");
83+
}
84+
return result.toString().trim();
85+
}
86+
}

0 commit comments

Comments
 (0)