I'm trying to be able to read the first line of my files which contains an int and then use that size for my array. After that I need to be able to fill and print my array with what is contained in the files. After running it, I get a out of bounds error.
import java.io.*;
import java.util.Scanner;
import java.util.Random;
public class LabAssignment4 {
public static void main(String[] args) throws IOException {
File animalsFile = new File("animals.txt");
Scanner animalsScan = new Scanner(animalsFile);
File soundsFile = new File("sounds.txt");
Scanner soundsScan = new Scanner(soundsFile);
int animalsSize = animalSize (animalsScan);
String [] animalsArray = new String [animalsSize];
int soundsSize = soundSize (soundsScan);
String[] soundsArray = new String [soundsSize];
fillAnimalsAndSoundsArray(soundsScan,animalsScan,animalsArray,soundsArray);
printAnimalsAndSoundsArray(animalsArray,soundsArray);
} // end main
public static int animalSize(Scanner animalsScan){
int animals = animalsScan.nextInt();
return animals;
}
public static int soundSize(Scanner soundsScan){
int sounds = soundsScan.nextInt();
return sounds;
}
public static void fillAnimalsAndSoundsArray(Scanner soundsScan, Scanner animalsScan, String [] animalsArray, String [] soundsArray){
int animalsIndex=0;
int soundsIndex=0;
while(animalsScan.hasNext()){
animalsArray[animalsIndex]=animalsScan.nextLine();
animalsIndex++;
}
while(soundsScan.hasNext()){
soundsArray[soundsIndex]=soundsScan.nextLine();
soundsIndex++;
}
}
public static void printAnimalsAndSoundsArray(String[] animalsArray, String[] soundsArray){
for(int i =0;i<animalsArray.length;i++){
System.out.println(animalsArray[i]);
}
for(int i = 0;i<soundsArray.length;i++){
System.out.println(soundsArray[i]);
}
}
} // end class