Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# ArrayIndexOutOf Bounds Exception error

Rauhl Roy
Ranch Hand
Posts: 401
hello all, below mentioned code is at bold is showing ArrayIndexOutOf BoundsException error but i need to add the +1 there because my codition is that i have to make sure my array numbers are in more than or equal to the previous number... example {1,3,4} should return true but {1,3,2} should return false.

pls give me the logic if you know

Jyoti Sri
Greenhorn
Posts: 12
I think there is a problem in your for loop

it should be, for (int i=0; i<a;i++)

So, According to your code this is happening :

1st iteration of for loop
i = 0
c = 0th element of the array i.e. 1
d= 1st element of the array i.e. 3

2nd iteration of for loop
i = 1
c = 1st element of the array i.e. 3
d= 2nd element of the array i.e. 4

After this i=2 which still satisfies your for loop condition , so one more iteration

3rd iteration of for loop
i = 2
c = 2nd element of the array i.e. 4
d= This tries to access 3rd element of the array , which is not there !!

thats why you are getting ArrayIndexOutofBound exception.

Hope this helps !!

Regards

David O'Meara
Rancher
Posts: 13459
"Jyoti",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with invalid display names get deleted.

Rauhl Roy
Ranch Hand
Posts: 401
thank you jyoti for your prompt reply.. but that is ok i solved it but the real problem is at in the for loop this line i have to compare the privous value in the array if previous value is <= than only i can return true or else method has to return false.

Originally posted by Jyoti:
I think there is a problem in your for loop

it should be, for (int i=0; i<a;i++)

So, According to your code this is happening :

1st iteration of for loop
i = 0
c = 0th element of the array i.e. 1
d= 1st element of the array i.e. 3

2nd iteration of for loop
i = 1
c = 1st element of the array i.e. 3
d= 2nd element of the array i.e. 4

After this i=2 which still satisfies your for loop condition , so one more iteration

3rd iteration of for loop
i = 2
c = 2nd element of the array i.e. 4
d= This tries to access 3rd element of the array , which is not there !!

thats why you are getting ArrayIndexOutofBound exception.

Hope this helps !!

Regards

Jyoti Sri
Greenhorn
Posts: 12

[ March 10, 2008: Message edited by: Jyoti S ]

[ March 10, 2008: Message edited by: Jyoti S ]
[ March 11, 2008: Message edited by: Jyoti S ]

Rauhl Roy
Ranch Hand
Posts: 401
no it does not work in all conditions.

rule is that we have to make sure {1,2,3} returns true but not {1,3,2}

Originally posted by Jyoti S:

[ March 10, 2008: Message edited by: Jyoti S ]

[ March 10, 2008: Message edited by: Jyoti S ]

Anubhav Anand
Ranch Hand
Posts: 341
Originally posted by Jyoti S:

Even this code will produce the ArrayOutOfBounds Exception. The problem is you have to make sure how many times should the loop execute.

Consider

Now the length of array is 3. If you loop for i<=length then it will go for 3 times, which means that when you try to get ith and i+1th element you'll be looking for i[3] and i[4] , so you see you are asking for something that doesn't exist. That's why the exception is coming. What you need to do is effectively think how many times the loop should go inorder to check all the elements.

Just a small hint is, you can loop less than array size e.g. i<length or i<length-1 etc.

Another, important thing to note is that checking same conditions multiple times doesn't make sense and is bad coding practice.
Just think for a second, in the condition is the a>=2 actually needed. What I mean is, why to check the same thing for many number of times, if you can check it once.

Why not this way

i.e. go for checking the scores, if and only if the aray size is more than or equal to 2.

One last thing. you are making new variables viz.

You can skip this by,

See no need to deal with multiple variables.
Hope that helps.
[ March 11, 2008: Message edited by: Anubhav Anand ]

Jyoti Sri
Greenhorn
Posts: 12
I think what you want to achieve is that the array should always contain numbers in increasing order eg {1,2,4,7,12} and thing like
{1,3,4,5,2} or {2,5,8,3} should return false.

So if i dry run my code for these inputs i think it gives correct results , can you give me a case where it doesn't works ?

no it does not work in all conditions.

rule is that we have to make sure {1,2,3} returns true but not {1,3,2}

public boolean scoresIncreasing(int[] scores) {
int a= scores.length;
boolean b = true;
for (int i=0; i<a;i++) { int c =scores[i];
int d= scores[i+1];
if (a>=2 && c>d)
{ b= false;
// If at any point in the array previous value is > then
// no need to check further just break for loop & return false
break;
}
}
return b;
}

[ March 11, 2008: Message edited by: Jyoti S ]

Ranch Hand
Posts: 72
try this,

Rauhl Roy
Ranch Hand
Posts: 401
hello jyoti

this conditions are faild..
({1, 3, 4})

({1, 1, 4})

({1, 1, 2, 4, 4, 7})

({-5, 4, 11})
so there is some flaw in the code.

Rauhl Roy
Ranch Hand
Posts: 401

your code is rocking really great, but can you find me where the mistake is in my code? just only one codition is faild from my code

and the codition faild was ({1, 1, 2, 4, 3, 7}) this

Rauhl Roy
Ranch Hand
Posts: 401
hi i did not put break in my code that is why my one codition was not sucessful.

Anubhav Anand
Ranch Hand
Posts: 341
The problem is with the if-else block

Even if your condition fails once, it will set the re to true if next condition is passed.

As in the case of {1, 1, 2, 4, 3, 7} it fails at 4<3 but gets true at 3<7.

That's the issue.

Ben Souther
Sheriff
Posts: 13411
"Jyoti S ",