• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array out of bounds exception

 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fellows,

I'm working on an assignment where I have to evaluates sequences and I think I've pretty much got most of the logic going, but I keep getting an out of bounds exception on the array.

Wondering if someone could lay another set of eyes to help me identify a fix to this. I don't have a whole lot of experience using arrays other than arraylists.

Thanks

Tester class:



Sequence Class:


Exception:


 
Paul Clapham
Sheriff
Pie
Posts: 20729
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Array entries are numbered starting from zero, not from one. So if an array has 5 entries, they are numbered from 0 to 4. So the way to iterate through the entries of an array is not this:

but this:

 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.. I am still getting the same error. It has something to do with line 70 of the sequence class. the: i-1

However, I thinking I need to evaluate for each time a new integer is added if it is the same as the entry before and after. Perhaps I could use better logic somewhere..

 
Paul Clapham
Sheriff
Pie
Posts: 20729
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assumed you had that code because you wanted to iterate through all the array entries. It appears you don't. So you should have said so, instead of just plugging in what I posted. So over to you. What are you trying to do with that code?
 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I still need to iterate through the array.. for every entry that goes into the array, I need to compare it and make sure it is equal or not equal to the next integer in the array. The return value needs to be all values in the array with parenthesis around like integers (222)4579(333)

I was trying to iterate through and compare the entry before and after. The problem is here:

Thoughts?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you probably want to iterate from element 1 to element size -1

this will start with the 2nd element, letting you compare it to the 1st and 3rd.

this will stop and the 2nd-to-last element, comparing it to the 3rd-to-last and last.

Note that you need to be careful if your array is of size 2. I think size 1 or 0 would be ok, as the loop wouldn't run at all...
 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so I've worked on this all day. I've got the system outs displaying correctly with the parenthesis correct for the sequences of the same numbers.

Now, here is where I'm stuck. The tester class is calling for a String to a method that returns a string. I've got the system outs but I cannot concatenate them into one string. Can someone help me?

Tester class with expected output:


Sequence Class:

 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UPDATE:

I've figured this out. All I had to do was create a StringBuffer.. Append everything to it as integers without parsing and then create another local variable and set it equal to the StringBuffer.toString(); or ..(name-of-variable.toString())

Ron
 
Winston Gutkowski
Bartender
Pie
Posts: 10091
56
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've figured this out. All I had to do was create a StringBuffer...

In the absence of any other requirement, you're generally better off using StringBuilder rather than StringBuffer, because it isn't synchronized. The APIs are otherwise identical.

Winston
 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! I've made an update.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic