• Post Reply Bookmark Topic Watch Topic
  • New Topic

Index out of bounds exception  RSS feed

 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all. I am struggling with this exercise from my book. My problem is that I can't even run the program, because it gives me index out of bounds exception, and to be honest I don't have an idea what exactly wentwrong.
I feel that even after it's able to run it will be totally wrong, but one thing at a time

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indexes in Java start with 0 and go up to one less than the length of an array. If I have an array with 3 elements in it, I can access it with arr[0], arr[1] and arr[2].

Keeping that in mind, see anything that is a problem in these two lines?

>
 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but I can't figure out how to fix it in order to work the way I want...
 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone? :?
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line is the problem, no?



But this line isn't a problem, right?



So why don't you do the same thing for the problem line which you did for the not-a-problem line?
 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know I probably look dumb now, but I don't really get it. I usually don't come here to ask for an answer, but in this case I can't figure it out even with your hints.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is the -1 in Paul's answer. (Or you could iterate until j<arr.length; note the missing =).
 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hauke Ingmar Schmidt wrote:It is the -1 in Paul's answer. (Or you could iterate until j<arr.length; note the missing =).

Yes, i tried this, but it doesn't seem to work...
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately "doesn't seem to work" isn't a very helpful problem description. Could you tell us what's wrong? You can read our FAQ entry ItDoesntWorkIsUseless for more information about how to do that.
 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Unfortunately "doesn't seem to work" isn't a very helpful problem description. Could you tell us what's wrong? You can read our FAQ entry ItDoesntWorkIsUseless for more information about how to do that.

After I changed this to this it kept giving me the index out of bounds exception. At least this is what i thought you meant. >
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the problem. Here's a simplified version of what you have:

There are a couple of things wrong here. The first gives you the IndexOutOfBoundsException, the second is just a logic error.

Firstly: Both for loops are iterating over the same array. The first loop starts at index 0 and stops at the last index, no problem here. The second loop however starts at 1 + the current index i, what happens when the first loop index is at the last index? The second loop starts and 1 + the last valid array index, which is invalid, and throws an IndexOutOfBoundsException.

Secondly: The condition on the second loop will cause the loop to exit before accessing the last element in the array. Try and spot the difference between the conditions on both of the for loops. The first one is technically correct but Hauke Ingmar Schmidt has suggested the more generally accepted format for that>
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a conventional format for iterating an array with a for loop. It readsThere is also a conventional format for iterating it backwardsThere is a good reason for sticking to those conventional formulae: they work and can be relied upon not to throw Exceptions.
 
Kat Rollo
Ranch Hand
Posts: 62
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There is also a conventional format for iterating it backwards

Hi, shouldn't it be i >= 0?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:Here's the problem. Here's a simplified version of what you have:


Firstly: Both for loops are iterating over the same array. The first loop starts at index 0 and stops at the last index, no problem here. The second loop however starts at 1 + the current index i, what happens when the first loop index is at the last index? The second loop starts and 1 + the last valid array index, which is invalid, and throws an IndexOutOfBoundsException.

Except that when the first loop index is at the last index (i will equal arr.length), j will be initialised to arr.length + 1, so the condition in the second loop (j < arr.length - 1) will fail and the loop will not run.
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne, you are quite right, and I am quite wrong.

A lesson to us all to test things for yourself rather than just running with the first thing that comes to mind that looks about right.

(Edit: Have a cow for successfully making me look like a bit of a ninny correcting my mistake)
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:(Edit: Have a cow for successfully making me look like a bit of a ninny correcting my mistake)

Thanks. To be honest I started writing a post saying the same thing, but fortunately noticed my mistake before hitting Submit.
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, I've now gone back and actually run the program for myself. After fixing the initial "i<=arr.length" problem I can see the next out of bounds problem.
This line:

throws this exception:

Now, "-3" is a value in your array. So somewhere in your code you are assigning the value of an element in your array to the variable "end" which you are then using as an array index. If you can find that then you'll be able to fix the exception.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kat Rollo wrote: . . .
Hi, shouldn't it be i >= 0?
Yes. Sorry for the mistake.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Werner has to review his whole algorithm. Apart from mixing array indexes with their actual values as pointed out by Tim, my fear is that he is not comparing the right adjacent values here:

What happens when 2 adjacent values are equal? I think he should rather compare arr[i] and arr[j].
- When arr[i]<=arr[j], increase tempCount by 1. Let i equal to j and continue the inner loop.
- When arr[i]>arr[j], let end=j+tempCount and exit the inner loop
- An array temparr of length arr.length could also keep the various tempCount after completion of the inner loop.
- longestSortedSequence will then return the maximum value of the temparr elements
 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the help.
I decided to leave it like this and make a new try. I came up with this(in case someone ever needs it(it's working)):
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicely done. I checked it very quickly and it seems to work!
Thanks for sharing.

Check what happens when you pass an empty array to this method.
Your instruction specifically says: Your method should return 0 if passed an empty array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!