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:

enter image description here

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 the startwith check. And delete what you added ... In general, there are a lot of problems. And in general, how to distinguish between am and j they will have the same code .--- exactly the table is correct? Or spaces for example need to arrange? - pavel
  • @pavel tell me a sign by which you can identify a specific letter, because let's say there is the letter "e", and there is the letter "f" and it turns out that "e" = "." and "f" = "..-.", this is confusing. How can one understand the length of a letter? - Silento
  • one
    Between the letters must be separators (pauses), otherwise nothing. - Roman

1 answer 1

According to Wikipedia :

Per unit of time is taken as the duration of one point. The duration of the dash is equal to three points. Pause between elements of one character - one point, between characters in a word - 3 points, between words - 7 points

Those. You did not consider that some of the characters in the Morse representation may contain other characters. You also need to add dividers between letters and words. So you will not have problems with the dismemberment of rows to individual characters.

You should also look for matches with the keys using the equals() method, and not contains() . So you will avoid incorrect matches.

  • I tried to do it through equals() - in this case, it does not find any coincidences at all. - Silento