0

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

1 Answers1

0

EDIT : I saw that you have changed your code with hasNextInt(), can you please upload the new code so we can see where you have changed it? And also, what do you mean by printing null? PointerNullException or nothing? Please provide us the error :)

To my understanding : Your file is shaped like this :

Line 1 Integer: corresponding to the number of animals/sounds1 in the file.
//Let's call the above number **nbFeatures** from now on
Line 2 String: corresponding to a single animal1 / sound1
Line 3 String: corresponding to a single animal2 / sound2
...

If it is not, can you add a few lines of your file please?

Now, the Out of Bound Exception indicates that there's an error while reaching an index in a array. (Usually, you try to reach index i of array, but the size of the array is smaller than i)

  • Check if nbFeatures in line 1 is equal to the actual number of lines in the file (minus the first line). In the ideal case, where the file only contains String of animal or String of sound, you can use this method to count the number of lines:

    int count = 0;
    //change animalScans to soundScans
    while (animalScans.hasNext()) {
       count++;
       animalScans.nextLine();
    }
    
    System.out.println(count-1) // -1 to not count the first line.
    

    If (count - 1) > nbFeatures, it means that you should either increase the size of your array to count-1 or change the method fillAnimalAndSoundArrays to fill the array until it is full.

If that's not the problem, can you upload the exception error please?