#Getting an regarding exceeding the length of the array, but I cant figure it out?
1 messages ยท Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
at gameBoard.loadProperties(gameBoard.java:27)
at gameBoard.<init>(gameBoard.java:15)
at Game.<init>(Game.java:25)
at Main.main(Main.java:9)
Detected code, here are some useful tools:
Exception in thread"main"java.lang.ArrayIndexOutOfBoundsException : Index4out of boundsfor length4at gameBoard.loadProperties(gameBoard.java : 27) at gameBoard.<init>(gameBoard.java : 15) at Game.<init>(Game.java : 25) at Main.main(Main.java : 9)
here is the method that it is suggesting the error is in but i cant figure it out
public void loadProperties() {
try {
Scanner reader = new Scanner(new File("boardSquares.txt"));
while (reader.hasNextLine()) {
String[] data = reader.nextLine().split(",");
String name = data[0];
int price = Integer.parseInt(data[1]);
int rent = Integer.parseInt(data[2]);
int location = Integer.parseInt(data[3]);
String details = (data[4]);
properties.add(new Property(name, price, rent, location, details));
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred. File with name 'boardSquares' does not exist");
}
}
Detected code, here are some useful tools:
public void loadProperties() {
try {
Scanner reader = new Scanner(new File("boardSquares.txt"));
while (reader.hasNextLine()) {
String[] data = reader.nextLine().split(",");
String name = data[0] ;
int price = Integer.parseInt(data[1] );
int rent = Integer.parseInt(data[2] );
int location = Integer.parseInt(data[3] );
String details = (data[4] );
properties.add(new Property(name, price, rent, location, details));
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred. File with name 'boardSquares' does not exist");
}
}
is the source file in there aswell ?
what do you mean?
all files are in the git repo
yes it is
looks like it's trying to read something that isnt thre
it works when i took out String details = (data[4] );
i added that now and it stopped working
i dont see where the array is set to a length of 4
so thats why im consfused
Go,0,0,1,
0 Go
1 -> 0
2 -> 0
3 -> 1
4-> ?
place a breakpoint on line 27 and start the program in debug mode, u will see that there is no number 4
( or actually use your catch to print the line next time ๐ )
do u know about debugging ?
no not really
basicly its a controlled pause so u can see what is happening while the program is running
even if you add value in the "4th" position it still doent work
ohhh ok thats pretty cool
Go,0,0,1,hiadadasdasdasdasdasdasdad
its not the same error right ? so u got progress
did u counter the emtpy lines at the end ?
right, want a rather nice trick for next time ?
Thanks soo much
sure
if u face such problems, esp with file parsing. Save the string it reads and force a printout on the catch clause
ill keep that in mind
shows on screen then ๐
catch (IndexOutOfBoundsException e) {
System.out.println(source + " length = " + source.length()); }