0
    String[] lvl15={"Twin", "Flank Guard", "Sniper", "Machine Gun"};
    String[] lvl30={"", "Triple Shot", "Quad Tank", "Twin Flank", "Tri-Angle", "Assasin", "Overseer", "Hunter", "Destroyer", "Gunner", ""};
    String[] lvl45={"Triplet", "Penta Shot", "Octo Tank", "Triple Twin", "Overlord", "Necromancer"};

    int index15;
    int index30;
    int index45;

    index15=rand.nextInt(lvl15.length);
    switch(index15){
        case 0: class15="Twin";
            break;
        case 1: class15="Flank Guard";
            break;
        case 2: class15="Sniper";
            break;
        case 3: class15="Machine Gun";
            break;
    }

    if(class15=="Twin"){index30=rand.nextInt(3)+1; class30=lvl30[index30];}
    if(class15=="Flank Guard"){index30=rand.nextInt(4)+2; class30=lvl30[index30];}
    if(class15=="Sniper"){index30=rand.nextInt(7)+5; class30=lvl30[index30];}
    if(class15=="Machine Gun"){index30=rand.nextInt(9)+8; class30=lvl30[index30];}

There is my code. For some reason it comes up with this error sometimes

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11
    at diepiogen.DiepIOGen.main(DiepIOGen.java:54)
    C:\Users\******\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
    BUILD FAILED (total time: 0 seconds)

So can someone please help because it seems like this is for no reason by the way line 54 is if(class15=="Sniper"){index30=rand.nextInt(7)+5; class30=lvl30[index30];}

And now im just adding filler because otherwise I cant post this.

mobinblack
  • 55
  • 7
  • 1
    Your array has a size of 11 so the index goes from 0 to 10 ... Error tells you that you are using index `11` which does not exist. – sinclair Jul 07 '16 at 16:31
  • 4
    `if(class15=="Twin")` [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) - I know that this part may work for you now since you are comparing string literals (which ware known at compilation time), but you should really stop using `==` to compare strings (especially if you will start comparing strings which ware created at runtime). – Pshemo Jul 07 '16 at 16:31
  • You are exceeding the range of the array by random generation with class30 – Andrew Li Jul 07 '16 at 16:31

2 Answers2

0

The math result from rand.nextInt(7)will be between 0-6, so randomly when you get 6 and adding 5, you gonna have 11. Your array has 11 element but the 11th has index of 10. So, change the code from index30=rand.nextInt(7)+5 to index30=rand.nextInt(7)+4 The Array index is going outside the bounds. See the java documentation: http://docs.oracle.com/javase/6/docs/api/java/util/Random.html#nextInt()

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
BigBugNoob
  • 53
  • 8
  • How can I have it generate a number in a range, like 3-6. – mobinblack Jul 07 '16 at 16:43
  • `rand.nextInt(7)` and if statement. `int random = rand.nextInt(7); if(random <4) random = rand.nextInt(7);`...this is not best solution where the probability is too low. or even better `rand.nextInt(4)+3`, but you have to think which one is incluse and exclusive – BigBugNoob Jul 07 '16 at 17:48
0

Your index is going outside the bounds of the array. from the java documentation: http://docs.oracle.com/javase/6/docs/api/java/util/Random.html#nextInt() it is indicated that nextInt(n) will get a new integer between the bounds of 0 inclusive to n exclusive (meaning from 0 to n-1). the list that you have made (lvl30) has 11 entries so if you add 5 to an int between 0 & 6 sometimes you get 11 which is out of bounds.