In the line b += morze[i]; The variable b assigned a link to a new line, the value of which is obtained by concatenating the old value b and morze[i] . From the fact that you assign a new string reference to variable b , the original string b1 does not change in any way. Therefore, it makes sense to collect the string from zero in the method and return it to return , after which save the resulting value to b1 :
public String decode(String a) { String b = "";
and
b1 = d.decode(a);
And now by the code itself:
- You are using the wrong algorithm to get the encoded string. It is necessary to go through all the characters of the source line and for each character to find the corresponding line in Morse code. Each time you can search for the index of the corresponding symbol in
text and take a string from morze with the same index, or you can create a HashMap for pairs of the символ -> строка Морзе . - The transformation of letters into Morse code characters, in my opinion, should be regarded as encoding, not decoding, so the
encode name is more suitable for the method. - It is quite wasteful to initialize the
text and morze with each method call. It makes sense to initialize them exactly once (either when the class is initialized or when the method is first called). To save money, a text array can be turned into an array of char . - Creating a new line at each iteration is also quite wasteful. For such purposes,
StringBuilder better suited.
As a result, the code can be rewritten as:
public class Decoder { private static final char[] chars = { 'a','b','c','d','e','f','g','h','i','j','k','l', 'm','n','o','p','q','r','s','t','u','v','w','x','y','z' }; private static final String[] morze = { ".-","_...","_._.","_..",".",".._.","__.", "....","..",".___","_._","._..","__","_.","___",".__.","__._","._." ,"...","-",".._","..._",".__","_.._","_.__","__.." }; private static final HashMap<Character, String> charToMorze = new HashMap<>(); static { for (int i = 0; i < chars.length; i++) { charToMorze.put(chars[i], morze[i]); } } public static String encode(String text) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); if (charToMorze.containsKey(c)) { sb.append(charToMorze.get(c)); } } return sb.toString(); } }
If the alphabet remains the same (from a to z ), then the code can be somewhat simplified:
public class Decoder { private static final String[] morze = { ".-","_...","_._.","_..",".",".._.","__.", "....","..",".___","_._","._..","__","_.","___",".__.","__._","._." ,"...","-",".._","..._",".__","_.._","_.__","__.." }; public static String encode(String text) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < text.length(); i++) { int index = text.charAt(i) - 'a'; if (index >= 0 && index < morze.length) { sb.append(morze[index]); } } return sb.toString(); } }
Using the Java 8 stream of s, the second option can be written like this:
public static String encode(String text) { return text.chars().map(c -> c - 'a').filter(c -> c >=0 && c < morze.length) .mapToObj(c -> morze[c]).collect(Collectors.joining()); }
b1 = d.decode(a, b1);wrote. Well, the algorithm is completely wrong ... - pavel