#Getting an regarding exceeding the length of the array, but I cant figure it out?

1 messages ยท Page 1 of 1 (latest)

frail frostBOT
#

<@&987246399047479336> please have a look, thanks.

frail frostBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

dreamy spade
#
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)
frail frostBOT
dreamy spade
#

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");
        }
    }
frail frostBOT
# dreamy spade ```java public void loadProperties() { try { Scanner reader...

Detected code, here are some useful tools:

Formatted code
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");
  }
}
dreamy spade
glass topaz
#

is the source file in there aswell ?

dreamy spade
#

all files are in the git repo

glass topaz
#

File("boardSquares.txt"));

#

that one

dreamy spade
#

yes it is

glass topaz
#

looks like it's trying to read something that isnt thre

dreamy spade
#

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

glass topaz
#

its 5

#

0 based

dreamy spade
#

yes

#

the length is not restricted tho

glass topaz
#

do u know what line it is, that it fails on ?

#

( besides the last 2 empty ones )

dreamy spade
#

it doesnt even run

#

after the first print which is medical monopoly it breaks

glass topaz
#

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

dreamy spade
#

what do you mean by breakpoint

#

comment it out?

glass topaz
#

( or actually use your catch to print the line next time ๐Ÿ˜‰ )

#

do u know about debugging ?

dreamy spade
#

no not really

glass topaz
#

basicly its a controlled pause so u can see what is happening while the program is running

dreamy spade
#

even if you add value in the "4th" position it still doent work

#

ohhh ok thats pretty cool

#

Go,0,0,1,hiadadasdasdasdasdasdasdad

glass topaz
#

its not the same error right ? so u got progress

dreamy spade
#

index 1

glass topaz
#

did u counter the emtpy lines at the end ?

dreamy spade
#

ohhh shoot

#

yea

#

that fixed it

glass topaz
#

right, want a rather nice trick for next time ?

dreamy spade
#

Thanks soo much

glass topaz
#

if u face such problems, esp with file parsing. Save the string it reads and force a printout on the catch clause

dreamy spade
#

bet

#

sounds good

glass topaz
dreamy spade
#

ill keep that in mind

glass topaz
#

shows on screen then ๐Ÿ˜‰

dreamy spade
#

ohhhhh alright

#

thats pretty cool

glass topaz
#
catch (IndexOutOfBoundsException e) {
            System.out.println(source + " length = " + source.length()); }
dreamy spade
#

betbet

#

let me save this quick

#

just took a quick screenshot

#

Thanks soo much