Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Error

 
Tom Grant
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting an error with this code when trying to read the file below
Code:


File:
Blocks: 1.1.1,1.2.1,1.3.1,1.1.2,1.2.2,1.3.2,1.1.3,1.2.3,1.3.3
any ideas what is going wrong? the error given is java.lang.ArrayIndexOutOfBoundsException: 0 for the line

And while im asking, what is the best way to save an id (which is an int) and an x and y co-ordinate (also int's) to a file? im sure this isnt it but i was just testing if i could do it.
 
Stephan van Hulst
Bartender
Pie
Posts: 5893
63
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tom. The problem is that String.split() does not work the way you think it does. Read the documentation again.

Unrelated to your problem, why does 'i' start with 1, and not with 0? Java arrays are zero-based.
 
Tom Grant
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Hi Tom. The problem is that String.split() does not work the way you think it does. Read the documentation again.

Unrelated to your problem, why does 'i' start with 1, and not with 0? Java arrays are zero-based.

its because when i was generating files they always started like Blocks: ,1.1.1... so the first split would be completely empty and i thought that could have caused the error
 
Alex Armenteros
Ranch Hand
Posts: 75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You suffered from a very common error we all had once on our programming lifes

Your error is here

strLine.replaceFirst("Blocks: ", "");

replaceFirst as well as concat, replace, substring etc Does NOT change the String object pointed by strLine (in this case)

Those functions create another string object and must be assigned to some variable (can be the same)

Also for a list of coordinates choose 2 symbols one for the axis and one to separate the points. (But avoid points as points are regular expressions and are a pain to use on functions like split)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic