Splitting a Split string throwing outOfBoundsException

Discussion in 'Plugin Development' started by jojodmo, Feb 1, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    jojodmo

    Hi, I'm trying to get multiple locations out of a config file, and I can't store them in more than one key. Here's my code:

    Code:java
    1. public void saveLoc(Location l){
    2. if(!Main.that.getConfig().contains("Locations")){
    3. Main.that.getConfig().set("Locations", "");
    4. }
    5. String w = l.getWorld().getName().toString();
    6. double x = l.getX();
    7. double y = l.getY();
    8. double z = l.getZ();
    9.  
    10. String s = Main.that.getConfig().getString("Locations").toString();
    11. Main.that.getConfig().set("Locations", s + "@" + w + ";" + x + ";" + y + ";" + z);
    12. }


    to save the location, and then to get the location, I'm trying to use:

    Code:java
    1. public boolean getLocs(){
    2.  
    3. String[] s = Main.that.getConfig().getString("Locations").split("@");
    4. for(int i = 0; i <= s.length; i++){
    5. String str = s[i];
    6. String[] loc = str.split(";");
    7. Location l = new Location(Bukkit.getWorld(loc[0]), (double) Integer.parseInt(loc[1]), (double) Integer.parseInt(loc[2]), (double) Integer.parseInt(loc[3]));
    8. }
    9. }[/i]


    So if I put in the Location "world", 5, -23, 172. It would put "world;5;-23;172" in the config file, and seperate all the other locations from it with @. When I get the locations, it should split all the locations apart, so if it was "world;5;-23;172@world;8;-23;1782@world;2;-23;1372", it would split it into "world;5;-23;172" and "world;8;-23;1782" and "world;2;-23;1372".

    Then, to get the location, it would split an already split string, "world;5;-23;172" into "world" and "5" and "-23" and "172". Then it would create a location using the world "world", x: 5, y: -23. and z:172. Yet whenever I call getLocs, I get this error:

    Code:
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    >      at net.podzol.jojodmo2010.plugin.Blocks.shouldRepel(Blocks.java:60) ~[?:?]
    >      at net.podzol.jojodmo2010.plugin.Blocks.creatureSpawn(Blocks.java:44) ~[?:?]
    >      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_10-ea]
    >      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_10-ea]
    >      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_10-ea]
    >      at java.lang.reflect.Method.invoke(Method.java:601) ~[?:1.7.0_10-ea]
    >      at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    >      ... 19 more
    
    Line 60 is where it creates the location, here:

    Code:java
    1. Location l = new Location(Bukkit.getWorld(loc[0]), (double) Integer.parseInt(loc[1]), (double) Integer.parseInt(loc[2]), (double) Integer.parseInt(loc[3]));


    So what I'm wondering is, why am I getting an outOfBoundsException, when the string should be split up into 4 strings? Thanks!
     
  2. Offline

    jojodmo

    [bump...]
     
  3. Offline

    Grief'd man

    Right here,
    Code:
     for(int i = 0; i <= s.length; i++){
    needs to be this:
    Code:
     for(int i = 0; i < s.length; i++){
    That equals sign will always return a value one above the length of the array, therefor making it out of bounds. At least, I think that's what your error is pointing at. That's the first thing I noticed.
     
  4. Offline

    igwb

    jojodmo2010
    Just for understanding:

    Arraya in almost all programming languages start to count at 0. So if you're String[] Array has one element it will be at position 0, but the size of the Array is 1. Therefore the "maximum position" is always one less than the size. Well, except for when it's empty.
     
Thread Status:
Not open for further replies.

Share This Page