I want to store a string to a two dimensional array and print the array but iam getting the array index out of bound exception but i don't know what gets wrong here can someone help me to find what goes wrong in my code.
This is the code i had tried,
public static void main(String[] args) {
Scanner sc= new Scanner (System.in);
System.out.println("Enter the string");
String str=sc.next();
char arr[][]=new char[5][5];
char a[]=str.toCharArray();
int l=a.length;
int k=0;
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
arr[i][j]=a[k];// this is the place where the error is occurred.
k++;
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
The error iam getting is,
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 24
at try2.Try2.main(Try2.java:27)