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. Can anyone assist me on this error please.
Here is my code
import java.util.Scanner;
03
04
public class ColTrans {
05
public static void main(String[] args)
06
{
07
String ori = "This is my horse";
08
String enc = encrypt(ori);
09
System.out.println(enc);
10
// String dec = decrypt(enc);
11
// System.out.println(dec);
12
}
13
14
static String encrypt(String text)
15
{
16
String result = "";
17
text = text.toUpperCase();
18
int length = text.length();
19
int rows = length/5;
20
char [][] b =new char[rows][5];
21
char [][] c =new char[5][rows];
22
char [] d = new char[length];
23
if ((length % 5) != 0)
24
rows = rows + 1;
25
int k = 0;
26
for(int i = 0; i < rows; i++)
27
for(int j = 0; j < 5; j++)
28
{
29
if ( k > length )
30
b[i][j] = 'Z';
31
else
32
{
33
d[k] = text.charAt(k);
34
b[i][j] = d[k];
35
}
36
k++;
37
}
38
for(int i = 0; i < 5; i++)
39
for(int j = 0; j < rows; j++)
40
{
41
c[i][j] = b[j][i];
42
result = result + c[i][j];
43
}
44
return result;
45
}
46
}