I wrote a simple Morse translator, but I ran into erroneous decryption / encryption. I do not understand where was wrong.
private Map<String, String> reverseMap = new HashMap<>(); private Map<String, String> baseMap = new HashMap<>(); private boolean isMorze = false; public void checkLanguage(String str) { boolean isLatinAlphabet = str.matches("^[a-zA-Z0-9]+$"); if (isLatinAlphabet) { translate(str); isMorze = false; } else { for (Map.Entry<String, String> entry : baseMap.entrySet()) { reverseMap.put(entry.getValue(), entry.getKey()); } isMorze = true; translate(str); } } public void translate(String start) { StringBuilder temp = new StringBuilder(); String result = ""; if (!isMorze) { for (Map.Entry<String, String> entry : baseMap.entrySet()) { if (start.contains(entry.getKey())) { temp.append(entry.getValue()); } } } if (isMorze) { for (Map.Entry<String, String> entry : reverseMap.entrySet()) { if (start.contains(entry.getKey())) { temp.append(entry.getValue()); } } } result = temp.toString(); System.out.println("---------------------------------------------------------------"); System.out.println("Yours string: " + start); System.out.println("In Morze it's look's like: "); System.out.print(result + "\n"); System.out.println("---------------------------------------------------------------"); result = ""; } public void init() { baseMap.put("a", ".-"); baseMap.put("b", "-..."); baseMap.put("c", "-.-."); baseMap.put("d", "-.."); baseMap.put("e", "."); baseMap.put("f", "..-."); baseMap.put("g", "--."); baseMap.put("h", "...."); baseMap.put("i", ".."); baseMap.put("j", ".---"); baseMap.put("k", "-.-"); baseMap.put("l", ".-.."); baseMap.put("m", "--"); baseMap.put("n", "-."); baseMap.put("o", "---"); baseMap.put("p", ".--."); baseMap.put("q", "--.-"); baseMap.put("r", ".-."); baseMap.put("s", "..."); baseMap.put("t", "-"); baseMap.put("u", "..-"); baseMap.put("v", "...-"); baseMap.put("w", ".--"); baseMap.put("x", "-..-"); baseMap.put("y", "-.--"); baseMap.put("z", "--.."); baseMap.put("1", ".----"); baseMap.put("2", "..---"); baseMap.put("3", "...--"); baseMap.put("4", "....-"); baseMap.put("5", "....."); baseMap.put("6", "-...."); baseMap.put("7", "--..."); baseMap.put("8", "---.."); baseMap.put("9", "----."); baseMap.put("0", "-----"); baseMap.put(" ", " "); } At the exit, I have the following:
UPD: having thrown off the code here, noticed that it adds all suitable letters to line.

start.contains- by this you break the whole order. Replace at least with thestartwithcheck. And delete what you added ... In general, there are a lot of problems. And in general, how to distinguish betweenamandjthey will have the same code.---exactly the table is correct? Or spaces for example need to arrange? - pavel