aspose file tools*
The moose likes Beginning Java and the fly likes Array out of bounds exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array out of bounds exception" Watch "Array out of bounds exception" New topic
Author

Array out of bounds exception

Ron Ingram
Ranch Hand

Joined: Mar 11, 2011
Posts: 63
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Mar 11, 2011
Posts: 63
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Mar 11, 2011
Posts: 63
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

Joined: Oct 02, 2003
Posts: 11313
    
  16

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...

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ron Ingram
Ranch Hand

Joined: Mar 11, 2011
Posts: 63
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

Joined: Mar 11, 2011
Posts: 63
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

Joined: Mar 17, 2011
Posts: 7784
    
  21

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

Joined: Mar 11, 2011
Posts: 63
Thank you! I've made an update.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array out of bounds exception