0

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)
Cheeku
  • 19
  • 5

3 Answers3

1

You need to check if k exceeds lenght of given input then don't add character into your array and break the loop, check the below code,

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++){
            if(k != l) {
              arr[i][j]=a[k];// this is the place where the error is occurred.
              k++;
            }else {
                break;
            }
        }
    }
    for(int i=0;i<5;i++){
        for(int j=0;j<5;j++){
            System.out.print(arr[i][j]+" ");
        }
        System.out.println();
    }
}
MOnkey
  • 751
  • 6
  • 13
  • if we do this then the same value will be printed again and again in the array. for example if the string is "welcome", then the output will be "w e l c o" will be printed five times in the new line but i need output to be printed as "w e l c o \n m e". I used \n to indicate that m and e should be printed in the new line. – Cheeku Feb 23 '20 at 07:17
  • hope that i had explained clearly, thanks in advance.! – Cheeku Feb 23 '20 at 07:22
  • Yes, your code really helped me. Thanks a lot,@MOnkey. – Cheeku Feb 23 '20 at 07:42
  • @Cheeku, could you please accept my answer by clicked on the tick – MOnkey Feb 23 '20 at 07:43
  • Sure bro,@MOnkey. – Cheeku Feb 23 '20 at 08:28
0

That is because of your k++

The inner loop runs 5 times, while the outer loop runs 5 times. Which makes the k++ execution to run 25 times. That is why you get ArrayIndexOutOfBoundException on a[k].

You should consider initializing your k value inside the first loop and outside the second loop. That would reset the value of k everytime the inner loops completes its iteration.

I hope that makes sense.

Safeer Ansari
  • 772
  • 4
  • 13
0

You are trying to access the data which is not available that is why you are getting the error. You are running the loop 25 times. To avoid that exception char a[25]=str.toCharArray() Instead char a[]=str.toCharArray()

Hope this help. If you like please up vote. Thank you.

Pravin Ghorle
  • 606
  • 7
  • 7