wood burning stoves*
The moose likes Beginning Java and the fly likes Array Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array Error" Watch "Array Error" New topic
Author

Array Error

Tom Grant
Greenhorn

Joined: Apr 06, 2012
Posts: 7
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

Joined: Sep 20, 2010
Posts: 3374
    
    9
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

Joined: Apr 06, 2012
Posts: 7
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

Joined: May 05, 2010
Posts: 67
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)
 
wood burning stoves
 
subject: Array Error
 
Similar Threads
Error printing out from a read in file.
How to find a if a file exists by extention only?
to read lines inside brackets
How to export 2D Array to a FIle
Regarding reading a file in a jsp