-2

I tried a practice for loop with the EXACT same format, and it won't result in an error message like this one does. And the inner loop (with int i) worked perfectly before I introduced the outer loop (with int j):

private static void mBTI(){
    Scanner console = new Scanner(System.in);
    Random rand = new Random();

    //Practice loop
    int wtf = console.nextInt();
    for (int k = 1; k <= wtf; k++){
        for (int m = 1; m <= 2; m++){
            System.out.println("derp");
        }
    }        

    //Error loop
    System.out.println("How many results do you want? (Type a numerical value.)");
    int amount = console.nextInt();
    String[] types = {"E","I","N","S","F","T","J","P"};
    int loop = 0;
    for(int j = 1; j <= amount; j++){
        for(int i = 1; i <= 4; i++){
            int randType = rand.nextInt(2);
            randType = loop+randType;
            System.out.print(types[randType]);
            loop = loop+2;
        }
        System.out.println("");
    }
}
Ray25Lee
  • 65
  • 1
  • 2
  • 8

1 Answers1

-1

You are probably gettin an index out of bounds exception. make sure to use randType = randType % types.length (or something else to keep randType in bounds).

PLEXATIC
  • 353
  • 1
  • 5