Greetings. I need your help again. I write a kind of program for encoding. There is a text that needs to be encoded. Let the array String[] text = {ace bdf} have a character set for encoding - final char[] KEY_WORDS = {'a','b','c','d','e','f'} . I run through the text and look for matches with the second array, and when I find it, I return the index of the match in the KEY_WORDS field to the newly created array. That is, I get a new array int[]total = {0,2,4,1,3,5} . There are no problems here. Now we need to decode it - take an array of total, and compare it with the KEY_WORDS array. But that's the problem - I need to compare the value with the total and the index in KEY_WORDS . thank

 public static ArrayList arrayCodeNumber(char[] s, char[] ss) { ArrayList total = new ArrayList(); for (int i = 0; i < s.length; i++) { for (int j = 0; j < ss.length; j++) { if (s[i] == ss[j]) { total.add(j); } } } return total; } public static void main(String[] args) throws FileNotFoundException { String filename = "C:\\a.java"; char[] s = read(filename).toCharArray(); final char[] KEY_WORDS = {'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', '_', '-', ' '}; ArrayList a = arrayCodeNumber(s, KEY_WORDS); System.out.println(a); String[] output = new String[a.size()]; for (int i = 0; i != a.size(); i++) { output[i] = (String) a.get(i); System.out.println(output); } for (int i = 0; i < output.length; i++){ if (output[i] == ??? ) } } 

}

  • 2
    Attach an example of your decision - JVic
  • one
    While admins think, I will ask you to make your question more readable. For this you need to put all your code inside special characters. When creating (editing) a question at the bottom there are tips on design. So your question will be much more susceptible. Thereby you will get an answer to it faster! - JVic
  • one
    for example, the first character is char c = KEY_WORLDS[total[0]); to get the entire row, loop through the total array - pavlofff
  • I'm sorry, I was in a hurry, now I’ll fix everything - RattenGW
  • I don’t understand much (and I don’t quite understand what you wrote. Is there exactly everything with brackets normal? - RattenGW

1 answer 1

  1. Cannot write String[] text = {ace bdf} . Need to either
    String[] text = {"ace bdf"}; ,
    or
    String[] text = {"ace", "bdf"}; .
    The first option does not look useful in your context - it is an array containing only one string.
  2. To decode, you do not need to compare the total array with the 'KEY_WORDS' array. It is enough to use the indices stored in the total array to extract elements from the 'KEY_WORDS' array, such as this:

     for (int i = 0; i < total.length; i++) System.out.print(KEY_WORDS[total[i]]) // 
  3. Care must be taken to ensure that no character in the input data can lead to an error. If for the next character from the input data there is no match in the 'KEY_WORDS' array, what will your program do - what value will it write in total and what will it do when it meets this value during decoding?

  4. In your approach, information about the structure of the input data (word splitting) is lost. If you need to keep this information, you should either mark the end of the word with a special code in total and process it accordingly when decoding, or treat the input data not as an array of words, but as one line containing and spaces, and encode / decode these spaces as well as other characters.
  5. I would put all the encoding / decoding into a separate class, with this, for example, interface:

     class Crypto { Crypto(Object[][] codeTable) { ... } // Таблица кодировки int encrypt(char plain) { .... } // Возвращает код символа char decrypt(int cipher) { .... } // Возвращает символ по его коду } 

    Which can be used somehow like this:

     Crypto encryptor = new Crypto({ {'a', 1}, {'b', 2}, // И так далее - таблица кодировки }); // ... ArrayList<Integer> cryptedData = new ArrayList<Integer>(); for (int i = 0; i < str.length(); i++) // str - Исходная строка cryptedData.add(encryptor.encrypt(str.charAt(i))); // шифруем // ... for (Integer cipher: cryptedData) System.out.print(encryptor.decrypt(cipher)); // Расшифровываем 

    This is rather clumsy and careless, just to demonstrate the idea, bring it to mind.
    Using a separate class will allow you to use it somewhere else, improve the algorithm, without touching the main program, etc. - a lot of advantages. For this OOP and come up with - use.

  • This is written before top-starter added the code to his question, so now the answer is not quite adequate to the question, but I will not rewrite it. Let him think how to apply this to his code. - m. vokhm
  • Yes, I understand that in the KEY_WORDS array, all valid characters should be listed, an example just had to be quickly set up, in my environment, I don't have all these minor issues. I am interested in decoding - the return of a character by an index. Your answer has shed light, but I'm just not familiar with such a "thing", KEY_WORDS [total [i]]. And in the future, I may leave the charms for single-character strings, and as KEY_WORD I will use just some texts in which all valid characters are found. Thanks again - RattenGW
  • 2
    @RattenGW, KEY_WORDS[total[i]] is simply extracting from the KEY_WORDS array a character that is in a position whose number (i.e. index) is equal to the value of the i-th element of the total array. In other words, total[i] is the next element of the cipher, and KEY_WORDS[total[i]] is the corresponding symbol of the source text. - m. vokhm
  • Thank you, I will remember this design. - RattenGW
  • Guys, how is it to shove a retour? )) I can't even crack you ... public char [] textAfterDeCoding (char [] KEY, int [] total) {for (int i = 0; i <total.length; i ++) {KEY [total [i]] ; } return; } I think I tried everything. And the variable was declared local, but from the fact that it is still not familiar with this normally - KEY [total [i]] seems to be the whole syringe. Tell me, what is it called? What would read about it ... - RattenGW