Please help, still a newbie with java and I have no idea why I keep getting the following error message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
Here is the method I am using:
public static ArrayList <Animal> createAnimalArrayList() {
String[] animalName = {"Skye", "Toby", "Peppa", "Charley", "Nemo", "Maisie"};
String[] ownerName = {"Joe Bloggs", "Mary Rice", "Ann Carroll", "Ciara Roddy", "Lllian Parks", "Ruth Jones"};
int[] animalAge = {10, 5, 1, 6, 2, 8, 3};
AnimalType[] animalType = {AnimalType.CAT, AnimalType.COW, AnimalType.DOG, AnimalType.GERBIL, AnimalType.HORSE, AnimalType.SHEEP};
ArrayList<Animal> animalList = new ArrayList<Animal>();
Random rand = new Random();
int randomNum = 0;
String aName = "";
String oName = "";
int aAge = -1;
AnimalType aType = null;
int randNumAnimals = rand.nextInt(4)+1;
for (int i = 0; i<randNumAnimals; i++) {
aName = animalName[rand.nextInt(6)];
oName = ownerName[rand.nextInt(6)];
aAge = animalAge[rand.nextInt(6)];
aType = animalType[rand.nextInt(6)];
animalList.add(new Animal(aName, oName, aAge, aType));
}
return animalList;
}
Any tips, or help would be greatly appreciated, I have looked through the method and can't tell why the code is not working.