• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Need some help on Arrays

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi so im having a small problem that i cant figure out and its that everytime i try to run my code im getting an ArrayIndexOutOFBoundsException around lines 60 and 61 and im not sure how to fix this can anyone help me with this?
Thank you
 
Marshal
Posts: 65425
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The basic format for iterating an array with a for loop is:-
for (int i = 0; i < myArray.length; i++) ...
That format will always visit every element of the array missing none out and will never overrun the bounds of the array however large or small it is. You should always start from that format and consider changes; if you use both myArray[i] and myArray[i + 1] you will need a change.
Now have a look at your lines 60 and 61 and see how you have written the for loops.
 
Greenhorn
Posts: 2
Eclipse IDE Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I noticed the formatting of your for loop on line 58
The maximum index for the array is 14 but the loop will set the count variable to 15 because of
I think if you get rid of the +1 you will get rid of your ArrayOutOfBoundsException because the loop will then run and terminate before count is set to 15.
 
Sheriff
Posts: 6182
163
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Elliot.
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!