• Post Reply Bookmark Topic Watch Topic
  • New Topic

I'm having a "java.lang.IndexOutOfBoundsException" and i don't know why.  RSS feed

 
Robson Martinz
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I'm having a "java.lang.IndexOutOfBoundsException" in the following code and i would like to know why. The idea is read a .txt file who has many numbers like this:
71 51 63 57 24 80 31
77 18 31 25 20 63 50
36 43 09 74 72 29 21
66 71 15 38 70 68 54
15 58 24 20 23 54 51
55 60 08 17 80 01 59
...
...

I would like to convert these numbers into Integer and put them in a Arraylist of array.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, first of all, the exception message will tell you exactly where it happens, and what index it's trying to index that is invalid. That's your first clue. What are they?

One question, which may or may not be relevant: why split(" ")? Are you sure that should be three spaces? It isn't tab-separated or anything? If you've got the delimiter wrong it would explain why the array was length 1 when you expected something else.
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

One important thing. Do not close resource used in try-with-resources (in this case br).
The try block will do that for you automatically.
Sometimes you might get IllegalStateException when try block is trying to close a resource already closed by you manually.
 
Robson Martinz
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown, the entire message is:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 14, Size: 14
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at leitor.Leitor.main(Leitor.java:32)
Java Result: 1


split("") is because the numbers are separated by tab-separated key, not space, so in the code i hit tab key between "".
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use "\t" in the split to make this more explicit.

It might help to make a smaller example so you can show all the lines in the file and what gets output. That way we can see where in the loop, you run into problems in relation to the output.
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for(int i=0; i < historic.size(); i++) {
for(int j=0; j < historic.get(i).length; i++) {
System.out.println(historic.get(i)[j]+ " ");
}
}


Look at the second for statement - do you really want to increment i or should this be j?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good catch Steve! Have a cow.
 
Robson Martinz
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the second for statement - do you really want to increment i or should this be j?

Oh my God, dude! Oh my god! I've lost hours looking for the error and didn't see that! Now, this loop is working as i expected. But why "ystem.out.println(historic.get(0).length);" still showing 1 in stead of 7?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robson Martinz wrote:But why "ystem.out.println(historic.get(0).length);" still showing 1 in stead of 7?

Because split isn't working as you intend. Probably has to do with the space vs tab problem described above. I recommend using split("\\w+") so you match any whitespace - space or tab.
 
Robson Martinz
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because split isn't working as you intend. Probably has to do with the space vs tab problem described above. I recommend using split("\\w+") so you match any whitespace - space or tab.


With "split("\\w+")" works fine, the "System.out.println(historic.get(0).length);" is 7 but the for loop for showing the numbers doesn't show nothing!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. I meant \\s+ and not \\w+


\s matches whitespace and \w matches word characters. So the reg exp I gave you matched the numbers and resulted in whitespace.
 
Robson Martinz
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, now everything is working fine as expected. Thank you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!