-3

Past few days Getting ArrayIndexOutOfBoundsException .I know this type of questions already asked on #SO . I tried .

java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

Code

String[] Child_DOB = "KUSHAGRA (SON)-07/05/94AANVI (DAUGHTER)-12/06/00 VARENYA (SON) - 26/12/05";
        ArrayList<String> children_List = new ArrayList<String>();
        ArrayList<Integer> Length_List = new ArrayList<Integer>();
        String Children_Details_str = "";

        int i = 0;
        while (i < Child_DOB.length) {
            String name_dob = Child_DOB[i] + "  " + Child_DOB[i + 1];//this line
            if (i > 3)
                Children_Details_str = Children_Details_str + "\n" + name_dob;
            else
                Children_Details_str = Children_Details_str + name_dob + "  ";
            children_List.add(name_dob);
            Length_List.add(Child_DOB[i].length());
            Length_List.add(Child_DOB[i + 1].length());
            i = i + 2;
        }

May I know what is the correct way to achieve my objective? Any help would be greatly appreciated

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
A B
  • 33
  • 9
  • before accessing you can check using if condition – Nikhil Sharma Apr 20 '17 at 06:35
  • It is hard to understand what you are trying to achieve. But you can check like this: `while ( (i + 1) < Child_DOB.length) { .. }` – Marat Apr 20 '17 at 06:36
  • String[ ] Child_DOB : incompatible type, it should be String. Split String and make String[ ]. – Janak Apr 20 '17 at 06:37
  • you have only one stringelements in "String[] Child_DOB" so it will have only Child_DOB[0] but not have elemts Child_DOB[i+1] so its throwing index out of exception – sunita Apr 20 '17 at 06:38
  • @Janak the value of `Child_DOB ` is hard coded – A B Apr 20 '17 at 06:38
  • @void provide the value you are use. – Janak Apr 20 '17 at 06:39
  • index i+1 doesn't exists – AbhayBohra Apr 20 '17 at 06:42
  • Pretty clear why you get the exception, you're accessing an element at index **1** while your array only contains **ONE** item on index **0**, what exactly are you trying to achieve? – Denny Apr 20 '17 at 06:44
  • Why getting down votes ?? – A B Apr 20 '17 at 06:48
  • Because the exception is clear, you even know on which line it is and you're not explaining what you're trying to achieve. I don't really see the point of posting for help when you're not even explaining what you're trying to achieve. We don't know what your objective is by saying `my objective`. – Denny Apr 20 '17 at 06:51
  • `String[] Child_DOB = "KUSHAGRA (SON)-07/05/94AANVI (DAUGHTER)-12/06/00 VARENYA (SON) - 26/12/05";` this isn't valid Java, your code doesn't compile. – Ryan M Sep 28 '21 at 04:19

5 Answers5

2

This is because of line as you pointed out:

String name_dob = Child_DOB[i] + "  " + Child_DOB[i + 1];//this line

Say you have only one element in your Child_DOB array, you would enter your while loop, and with i = 0 try to access i+1 i.e. 1st element. Array starts with 0th index and hence accessing element at index 1 would throw ArrayIndexOutOfBoundsException. To avoid this one option would be:

while (i < Child_DOB.length - 1) {//go until second last element

This would work if your array has even number of elements. If you have odd number of elements then you would miss the last name (which should be fine as per your logic.)

if you really dont want to miss the last element, you could do something like:

if ((Child_DOB.length & 0X1) == 1) {
    Length_List.add(Child_DOB[Child_DOB.length - 1].length());
    //append to Children_Details_str.. better use StringBuilder here
}
SMA
  • 36,381
  • 8
  • 49
  • 73
2

Change your while loop condition as:

 while (i < Child_DOB.length - 1)

Explanation:

For example, Child_DOB.length is 5 and i value is 4, In your code:

    int i = 4;
    while (i < 5) {
        String name_dob = Child_DOB[4] + "  " + Child_DOB[4 + 1]; 

Here Child_DOB[5] causes ArrayIndexOutOfBoundsException because array index start from 0 and your Child_DOB index's are [0 1 2 3 4].

Hope this will help~

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
1

There is only one String in CHILD_DOB. In your while statement you typed Child_DOB[i + 1]; which accesses CHILD_DOB[1] but there is only one item in CHILD_DOB which is CHILD_DOB[0] Try this

String[] Child_DOB = new String[] {"KUSHAGRA",
 "(SON)-07/05/94AANVI", "(DAUGHTER)-12/06/00", "VARENYA", "(SON)", "-", "26/12/05"};

Which I think would fix your code.

Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
Abdilahi
  • 11
  • 2
1

You have only one element in Child_DOB so Child_DOB.length=1, and inside while loop you are trying to access 2nd element Child_DOB[i + 1] in first iteration itself... So you are getting exception... Can you tell exactly what output you want??

1

Use-

while (i < Child_DOB.length - 1) 

Instead of -

while (i < Child_DOB.length)
AGM Tazim
  • 2,213
  • 3
  • 16
  • 25