Adding to String[] in Iteration?

Discussion in 'Plugin Development' started by hanahouhanah, Aug 8, 2014.

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

    hanahouhanah

    Is it possible and how would I go about doing it?
    I wrote this out:
    Code:java
    1. for(int size = 0; size<getConfig().getList("maps").size(); size++)
    2. {
    3. int am = size;
    4. int num = am+1;
    5. Object[] z = getConfig().getList("maps").toArray();
    6. if(first)
    7. {
    8. votes[am] = "0";
    9. if(sizer < size)
    10. {
    11. sizer++;
    12. }
    13. else
    14. {
    15. first = false;
    16. }
    17. }
    18. pl.sendMessage(ChatColor.GREEN + "" + num + ".) " + z[am].toString() + " -||- " + votes[am].toString() + " Votes");
    19. }


    Doesn't work like I thought, NPE.
    I need it to work this way because the two lists have to have aligned indexes (if that makes any sense). Basically, in list1[0] and list2[0], they should have information that's connected.
    Pretty sure the way I'm doing it is the wrong way, but I'm not sure how to go about doing it any other way.
     
  2. Offline

    Chiller

    hanahouhanah The best way to do what your trying to do is to have two arraylists, because with regular arrays they have a set length, can not be modified.
     
  3. Offline

    _LB

    What are you trying to do? Once you create an array, it cannot be resized. Use an ArrayList.
     
  4. Offline

    hanahouhanah

    Chiller _LB
    Aah, I tried to do that earlier but I guess I thought it wouldn't work because it was a mess of errors since I made the rest of the stuff to work with the reg. array. After a bit of translating it I made it work.
    Thanks a bunch! Sorry for the late reply, I normally believe I won't get replies so quickly when I make threads.
    Unfortunately I found this error while converting it:
    votes.get(am) (used to be votes[am].toString())- it gives an IndexOutOfBoundsException
    -
    This is how it looks now:
    Code:java
    1. for(int size = 0; size<getConfig().getList("maps").size(); size++)
    2. {
    3. int am = size;
    4. int num = am+1;
    5. Object[] z = getConfig().getList("maps").toArray();
    6. if(first)
    7. {
    8. votes.add("0");
    9. if(sizer < size)
    10. {
    11. sizer++;
    12. }
    13. else
    14. {
    15. first = false;
    16. }
    17. }
    18. p.sendMessage(ChatColor.GREEN + "" + num + ".) " + z[am].toString() + " -||- " + votes.get(am) + " Votes");
    19. }

    If I were to comment out votes.get(am), it will display the list "maps" correctly, which confuses me because I shouldn't be getting an exception like that if it is populated. I tried running a debug through it just before it sends the message to see what's in "votes" and I got the following:
    [0]
    [0]
    Which I'm pretty sure is how it should look.
    Since it might be asked, here is the entire stack trace (with the debug)
    Code:
    [22:41:49 INFO]: [0]
    [22:41:49 INFO]: [0]
    [22:41:49 WARN]: [LastManStanding] Task #482 for LastManStanding v1.0 generated
    an exception
    java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
            at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_05]
            at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_05]
            at com.lfs.hanah.dodatang.LastFlagStanding$2.run(LastFlagStanding.java:1
    01) ~[?:?]
            at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftTask.run(CraftTask.java
    :53) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-63-g8356f65-b3075jnks]
            at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:345) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-63-g8356f
    65-b3075jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    00) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-63-g8356f65-b3075jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-63-g8356f65-b3075jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-63-g8356f65-b3075jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-63-g8356f65-b3075jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-63-g8356f65-b3075jnks]
    >
    Thanks again for the help, Chiller and _LB, as well as anyone else who at least took a look.
     
  5. Offline

    _LB

    Format your code properly:
    Even with correct formatting this code is still very very confusing.

    Why do you confusingly call it "size" instead of "i"?
    Why do you create "am" instead of just using "size"?
    Why do you create "num" instead of just using "size+1"?
    Why do you create "z" before the if statements?
    Where does "sizer" come from?
    You realize that if "first" is false then it will stop adding to "votes" but you will still try to get elements from "votes" that do not exist? This is the cause of your exception.

    You need to clean up your code some, it's a mess ;)
     
Thread Status:
Not open for further replies.

Share This Page