简体   繁体   中英

Exception in thread main ArrayIndexOutOfBoundsException

Can someone maybe help me with the following error. Exception in thread main java.lang.arrayindexoutofboundsexception : 3 at RowTrans.encrypt(Rowtrans.java:33) at RowTrans.main(Rowtrans.java :7)

In my program I want to get a text. Put it in a matrix with 5 columns and determine the rows according to the length of the text. Then i want to change the column and row position so that the row gets the columns position and the column the row. And when a row does not contain 5 values I want to add the character Z in the empty spaces. Can anyone assist me on this error please.

Here is my code

import java.util.Scanner;

public class ColTrans {

   public static void main(String[] args)
   {
      String ori = "This is my horse";
      String enc = encrypt(ori);
      System.out.println(enc);
      // String dec = decrypt(enc);
      // System.out.println(dec);
   }

   static String encrypt(String text)
   {
      String result = "";
      text = text.toUpperCase();
      int length = text.length();
      int rows = length / 5;
      char[][] b = new char[rows][5];
      char[][] c = new char[5][rows];
      char[] d = new char[length];
      if ((length % 5) != 0)
         rows = rows + 1;

      int k = 0;
      for (int i = 0; i < rows; i++)
         for (int j = 0; j < 5; j++)
         {
            if (k > length)
               b[i][j] = 'Z';
            else
            {
               d[k] = text.charAt(k);
               b[i][j] = d[k];
            }

            k++;
         }

      for (int i = 0; i < 5; i++)
         for (int j = 0; j < rows; j++)
         {
            c[i][j] = b[j][i];
            result = result + c[i][j];
         }

      return result;

   }
}

Here is the cause:

You are increamenting row variable by one, once you have defined the array.

Move following line before line char [][] b =new char[rows][5];

if ((length % 5) != 0)

      rows = rows + 1;

There are 2 issues in your code. First move the mod part before the matrix instantiation :

  if ((length % 5) != 0)
     rows = rows + 1;

   char [][] b =new char[rows][5];
   [...]

Then change if ( k > length ) to if ( k >= length )

just change your code as following:

if ((length % 5) != 0)
   rows = rows + 1;
char[][] b = new char[rows][5];
char[][] c = new char[5][rows];
char[] d = new char[length];

According to your description:

text = text.toUpperCase();
    char[] b = text.toCharArray();
    char[][] c = new char[b.length][5];

    int bLen = 0;
    for (int i = 0; i < c.length; i++) {
        for (int j = 0; j < 5; j++) {
            if(bLen < b.length)
                c[i][j] = b[bLen++];
            else
                c[i][j] = 'Z';

        }
    }

 //change the column and row position 
 char[][]d = new char[c[0].length][c.length];

    for (int i = 0; i < d.length; i++) {
        for (int j = 0; j < d[0].length; j++) {
            d[i][j] = c[j][i];

        }
    }

Output: TI EZZZZZZZZZZZZHSHZZZZZZZZZZZZZI OZZZZZZZZZZZZZSMRZZZZZZZZZZZZZ YSZZZZZZZZZZZZZ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM