• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array Index Out Of Bounds Exception  RSS feed

 
barry richard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am trying to run this



I get



I am fairly certain that there is no errors in there, What could the problem be?

Thanks
 
barry richard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just some more explanation, I am sure you pros can see it just by looking but What I am trying to do is to read the Array and add a number to each value and then print it

thanks
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
barry richard wrote:
I am fairly certain that there is no errors in there


I should hope that after seeing the error message, you would be completely certain that there are errors.

Just like in your other thread, the error message is telling you exactly what's wrong.

It's telling you that at line 13 of Example.java, you are trying to access the 6th element (at index 5) of an array that has less than 6 elements (and therefore indices 0..4 at the most).
 
barry richard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Resolved I needed to remove = for <=.

Can anybody tell me why = needs to be removed?
 
barry richard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff,

But why <= did not work?
Thanks
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
barry richard wrote:Thanks Jeff,

But why <= did not work?
Thanks


Well, what's the difference between < and <=?

Is 4 < 5?
Is 4 <=5?

Is 5 < 5?
Is 5 <= 5?

Now look again at this:


Do you understand what the count <= (or <) x.length part means?

 
barry richard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I get it, it is an issue with index numbers being less than the length

like:
length = 5
index 0 to 4

Thanks for your help now I am clear on the other post error as well.

Thanks again
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. Glad you got it.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>> x[count]+=5;


your array has only 5 elements
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The standard idiom for running through an array in Java is this:



The vast majority of for-loops you'll write in Java are this one. (In fact I can't remember the last time I wrote a for-loop which did anything but that.)

However you're in school, so I guess you're supposed to learn all of the other possibilities which you might use a for-loop to implement. So go back to your text and read carefully about what the three parts of the for-loop do (the three parts separated by semicolons, that is) and when they do it.

Or if you're more of an experimentalist, try some code:


 
Paul Witten
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
barry richard wrote:Just some more explanation, I am sure you pros can see it just by looking but What I am trying to do is to read the Array and add a number to each value and then print it

Barry, one thing that may help you parse these exceptions is that they don't lie. Instead of trying to force exceptions to conform to what you believe should be reality, it's better to take the opposite slant. The exceptions and stacktraces are the authorities. What they say is "The Bible" so to speak.

It's very helpful to step through the code as it executes. Most IDEs have excellent debuggers where you can watch values change as each loop rolls by. You will see then exactly where and why it explodes.

Otherwise, I used to insert test code into my code to do System.out.println() as each loop went by. That's another way to see how the progression goes without using a debugger.
 
barry richard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys I get it now.

Thanks for the advice
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:>>> x[count]+=5;

. . .
Why did you write >>> ? That is an operator in its own right, so please avoid using it as a line start symbol.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sure.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!