第2D阵列在循环中出于绑定的异常



我正在研究关键字柱形密码,并且我一直从限制的异常中获得数组,我尝试调试代码并尝试抓住问题以了解问题,但我无法!<<<<<</p>

public Decryption (String cipherText, String keyWord) {
      cipherText = cipherText.replaceAll("\s+","");
      cipherText = cipherText.toUpperCase();
      cipherText = cipherText.trim();
      keyWord = keyWord.toUpperCase();
      int column = keyWord.length();
      int row = (cipherText.length() / keyWord.length());
        if (cipherText.length() % keyWord.length() != 0)
          row += 1;
      char [][] matrix = new char [row][column];
      int re = cipherText.length() % keyWord.length();
       for (int i = 0; i < keyWord.length() - re; i++)
         matrix[row - 1][keyWord.length() - 1 - i] = '*';
      char[] sorted_key = keyWord.toCharArray();
      Arrays.sort(sorted_key); 
      int p = 0, count = 0; 
      char[] cipher_array = cipherText.toCharArray();
      Map<Character,Integer> indices = new HashMap<>();
      for(int i = 0; i < column; i++){
       int last = indices.computeIfAbsent(sorted_key[i], c->-1);
        p = keyWord.indexOf(sorted_key[i], last+1);
          indices.put(sorted_key[i], p);
           for(int j = 0; j < row; j++){
            if (matrix[j][p] != '*') 
            matrix[j][p] = cipher_array[count];
                    count++; 
                }}
}

我得到的例外是:

matrix[j][p] = cipher_array[count];

循环存在问题,如果我从j = 1开始,则不会给我异常,但我没有得到正确的结果(它没有打印最后一行)

我要解密的密码文本:

yaruedcauoadgryhobbnderpustkntttglorwungefuolndrdeygooaoaojruckespy

关键字:

你自己

用1:

开始循环时得到的结果

判断自己的背景知识以了解CRYP

我应该得到的:

判断自己的背景知识要了解 加密

我不确定,因为您的代码不允许我对此进行验证(没有简单的方法可以在不进行挖掘的情况下检查算法的输出),所以...假设解决方案是:

for (int j = 0; j < row; j++) {
            if (matrix[j][p] != '*'){
                matrix[j][p] = cipher_array[count];
                count++;
            }
        }

而不是:

for (int j = 0; j < row; j++) {
                if (matrix[j][p] != '*')
                    matrix[j][p] = cipher_array[count];
                    count++;
            }

我认为在这种情况下,将'*'附加到字符串的策略不是要走的路 - 就像您所做的那样。最好在建立网格时附加一些角色。

遵循此方法是您的代码的固定版本(检查更改部分的代码中的注释):

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Decryption {
    private final String result;
    public Decryption(String cipherText, String keyWord) {
        cipherText = cipherText.replaceAll("\s+", "");
        cipherText = cipherText.toUpperCase();
        cipherText = cipherText.trim();
        keyWord = keyWord.toUpperCase();
        int column = keyWord.length();
        int row = (cipherText.length() / keyWord.length());
        if (cipherText.length() % keyWord.length() != 0)
            row += 1;
        int[][] matrix = new int[row][column];
        // Changed to calculate the irregular columns
        int re = column - (row * column - cipherText.length());
        char[] sorted_key = keyWord.toCharArray();
        Arrays.sort(sorted_key);
        int p, count = 0;
        char[] cipher_array = cipherText.toCharArray();
        Map<Character, Integer> indices = new HashMap<>();
        for (int i = 0; i < column; i++) {
            int last = indices.computeIfAbsent(sorted_key[i], c -> -1);
            p = keyWord.indexOf(sorted_key[i], last + 1);
            indices.put(sorted_key[i], p);
            // Changed: Detects the need of an extra character and fills it in case of need
            boolean needsExtraChar = p > re - 1;
            for (int j = 0; j < row - (needsExtraChar ? 1 : 0); j++) {
                matrix[j][p] = cipher_array[count];
                count++;
            }
            if(needsExtraChar) {
                matrix[row - 1][p] = '-';
            }
        }
        result = buildString(matrix);
    }
    public static void main(String[] args) {
        System.out.println(new Decryption("EVLNE ACDTK ESEAQ ROFOJ DEECU WIREE", "ZEBRAS").result);
        System.out.println(new Decryption("EVLNA CDTES EAROF ODEEC WIREE", "ZEBRAS").result);
        System.out.println(new Decryption("YARUEDCAUOADGRYHOBBNDERPUSTKNTTTGLORWUNGEFUOLNDRDEYGOOAOJRUCKESPY", "YOURSELF").result);
    }
    private String buildString(int[][] grid) {
        return Arrays.stream(grid).collect(StringBuilder::new, (stringBuilder, ints) -> Arrays.stream(ints).forEach(t -> {
            stringBuilder.append((char) t);
        }), (stringBuilder, ints) -> {
        }).toString().replace("-", "");
    }
}

如果您运行此功能,则将打印:

WEAREDISCOVEREDFLEEATONCEQKJEU
WEAREDISCOVEREDFLEEATONCE
JUDGEYOURSELFABOUTYOURBACKGROUNDKNOWLEDGETOUNDERSTANDCRYPTOGRAPHY

最新更新