-1

I am looking for some help with my code. I want the program to read from a .txt file and create an Animal object and insert it into an ArrayList of Animal objects, based upon the data found in the file, with the presumption that the data in the file is formatted correctly.

I created a counter, to keep track of the line number. Since I know how the data in the .txt file is formatted, I can anticipate where one Animal object begins and another ends.

The issue I keep getting is that the animal objects in the ArrayList have the same fields as the last Animal in the text file. Any and all help is greatly appreciated.

BufferedReader userInputFile = null;
int ctr = 1;
try {
    userInputFile = new BufferedReader(new FileReader(userDocFilePath));
    Animal newFileAnimal = new Animal();

    while((s = userInputFile.readLine()) != null) {                                                
        if (ctr == 1){
            newFileAnimal.setName(s);
            ctr++;
        } else if (ctr == 2) {
            newFileAnimal.setSpineStatus(s);
            ctr++;
        } else if (ctr == 3) {
            newFileAnimal.setFurStatus(s);
            ctr++;
        } else if (ctr == 4) {
            newFileAnimal.setSwimStatus(s);
            System.out.printf("Animal name: %s\n\tSpine status: %s\n\t"
                + "Fur status: %s\n\tSwim status: %s\n", Animal.getName(), 
                + newFileAnimal.getSpineStatus(), 
                + newFileAnimal.getFurStatus(),
                + newFileAnimal.getSwimStatus());
            array.add(newFileAnimal);
            ctr = 1;
        }
    }

The text file looks like this:

Rabbit
true
true
true
Duck
true
false
true
  • 1
    Your issue is that you don't update the local variable 'newFileAnimal', you create only one instance of an Animal and always update that one, resulting in all of the objects you're placing into the array being the same Animal and the data is with the last info placed. – L33T Apr 17 '17 at 06:22

2 Answers2

1

That's because you reuse the same Animal object over and over again. It's values are overwritten when the next animal is read in. At the end you get the values of the last animal.

The list contains n references to one and the same Animal object.

To solve this, create a new object for each animal.

Henry
  • 42,982
  • 7
  • 68
  • 84
0

If I understood your problem correctly. You are getting below result in ArrayList:

Duck
true
false
true
Duck
true
false
true

If this is the case then you'll need to move your Animal object initialization inside your while loop.

userInputFile = new BufferedReader(new FileReader(userDocFilePath));
Animal newFileAnimal = null;

while((s = userInputFile.readLine()) != null) {    
    newFileAnimal = new Animal();                                            
    if (ctr == 1){
        newFileAnimal.setName(s);
        ctr++;
    } else if (ctr == 2) {
        newFileAnimal.setSpineStatus(s);
        ctr++;
    } else if (ctr == 3) {
        newFileAnimal.setFurStatus(s);
        ctr++;
    } else if (ctr == 4) {
        newFileAnimal.setSwimStatus(s);
        System.out.printf("Animal name: %s\n\tSpine status: %s\n\t"
            + "Fur status: %s\n\tSwim status: %s\n", Animal.getName(), 
            + newFileAnimal.getSpineStatus(), 
            + newFileAnimal.getFurStatus(),
            + newFileAnimal.getSwimStatus());
        array.add(newFileAnimal);
        ctr = 1;
    }
}

Edit:

The reason of creating new objects is that, ArrayList instead of creating a new Object and storing, it stores the references of the objects so every time you overwrite the object, all the references points to the update Animal object.

this might help

Community
  • 1
  • 1
Ashyboy
  • 163
  • 1
  • 8
  • This is what I had originally thought, however, the issue that this leads to is that a new Animal object is created for every line. – prototype7861 Apr 17 '17 at 06:27
  • @tenebris_lumen You _want_ a separate `Animal` object to encapsulate each animal you read from your file (I think). – Tim Biegeleisen Apr 17 '17 at 06:28
  • @tenebris_lumen Well this is how it will work in ArrayList. ArrayList uses the references of the objects it stores in it instead of creating a new one. So you'll need to create a new object for every Animal. – Ashyboy Apr 17 '17 at 06:33