As a result of the method, the variable String b1 should be assigned the value b . But this is not happening.

 public class Decoder { public String decode(String a,String b) { CharSequence[] text = { "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" }; String[] morze = { ".-","_...","_._.","_..",".",".._.","__.","...." ,"..",".___","_._","._..","__","_.","___",".__.","__._","._." ,"...","-",".._","..._",".__","_.._","_.__","__.." }; for (int i = 0; i < text.length; i++) { if (a.contains(text[i])) { b += morze[i]; } } System.out.println(":" + b); return b; } } soundButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String a = textText.getText(); String b1 = ""; d.decode(a, b1); System.out.println(b1); morzeText.setText(b1); s.play(); } }); 
  • one
    uh ... and why should it be assigned? you would at least b1 = d.decode(a, b1); wrote. Well, the algorithm is completely wrong ... - pavel

1 answer 1

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:

  1. 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 символ -> строка Морзе .
  2. 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.
  3. 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 .
  4. 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()); } 
  • and even the chars array could have been removed altogether, and the HashMap too. Than you like sb.append(morze[c - 'a']) did not like it. - pavel
  • @pavel was such a thought. However, if suddenly the author decides to add more characters to the alphabet, this approach will break. Therefore, the option with subtraction, I think, will add as an alternative with the note "if you are not going to change the alphabet and the order of the characters in it." - Regent