• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0  RSS feed

 
Apple Davisison
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am a beginner in java and I'm have a hard time on how to get rid of this error. please help. Here's the code:


 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you post code please add code tags -> https://coderanch.com/wiki/659781/Wiki/Code-Tags
There, doesn't that look better?
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java arrays are zero based. An array of size N has indices from zero to N-1.

When i == k this will throw an exception because you've gone beyond (k-1).
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
I don't understand why you are looping up to 1000 in line 27. You will start off with a 0‑element array and as CB has already told you, you can only access members of an array up to length - 1, so you could only use element -1, but you can't access negative indices. In fact you are trying to find an element in a 0‑length array, which it hasn't got. So you will get an Exception.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!