title: 2d array out of bound exception in for-loop
body: <p>I'm working on keyword columnar cipher and I keep getting array out of bound exception, I have tried debugging the code and try and catch to understand the problem but I couldn't!</p>
  
  <pre><code>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 &lt; 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&lt;Character,Integer&gt; indices = new HashMap&lt;&gt;();
  
        for(int i = 0; i &lt; column; i++){
  
         int last = indices.computeIfAbsent(sorted_key[i], c-&gt;-1);
          p = keyWord.indexOf(sorted_key[i], last+1);
            indices.put(sorted_key[i], p);
  
             for(int j = 0; j &lt; row; j++){
              if (matrix[j][p] != '*') 
              matrix[j][p] = cipher_array[count];
                      count++; 
                  }}
  }
  </code></pre>
  
  <p>I'm getting the exception in:</p>
  
  <pre><code>matrix[j][p] = cipher_array[count];
  </code></pre>
  
  <p>there is a problem with the loop, if I start with j = 1 it doesn't give me the exception but I don't get the correct results (It doesn't print the last row)</p>
  
  <p>The cipher text that I'm trying to decrypt:</p>
  
  <blockquote>
    <p>YARUEDCAUOADGRYHOBBNDERPUSTKNTTTGLORWUNGEFUOLNDRDEYGOOAOJRUCKESPY</p>
  </blockquote>
  
  <p>Keyword:</p>
  
  <blockquote>
    <p>YOURSELF</p>
  </blockquote>
  
  <p>The result I get when I start the loop with 1:</p>
  
  <blockquote>
    <p>JUDGE YOURSELF ABOUT YOUR BACKGROUND KNOWLEDGE TO UNDERSTAND CRYP</p>
  </blockquote>
  
  <p>What I'm supposed to get:</p>
  
  <blockquote>
    <p>JUDGE YOURSELF ABOUT YOUR BACKGROUND KNOWLEDGE TO UNDERSTAND
    CRYPTOGRAPHY</p>
  </blockquote>