I'm trying to complete an assignment for a Java class that I am currently taking but I'm having some issues with creating an array of objects (Giraffes in this case) from a text file. Can anyone provide some advice for me?
The issue that I am having is that when I try to print the values of the ArrayList or converted Array I get stuff like [giraffe.Giraffe2@55f96302, giraffe.Giraffe2@3d4eac69] instead of the values from the text file.
I've created a class called Giraffes in multiple different ways, what seems to make the most sense is to use an array to store the values of the constructors and set the values from the array.
package giraffe;
public class Giraffe2 {
protected String birthLocation, sire, dam, subSpecies, zoo, city,
state,event, name,
localId, birthDate, sex;
protected int giraffeId;
public void Giraffe2(String array[]){
this.giraffeId = array[0];
this.sex = array[1];
this.birthDate = array[2];
this.sire = array[3];
this.dam = array[4];
this.birthLocation = array[5];
this.localId = array[6];
this.name = array[7];
this.subSpecies = array[8];
this.zoo = array[9];
this.city = array[10];
this.state = array[11];
this.event = array[12];
}
}
The code that I am using to create the Giraffes is:
package giraffe;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class HW3 {
public static void main(String[] args) throws IOException {
//Create an ArrayList that will hold the Giraffes
ArrayList<Giraffe2> giraffes = new ArrayList<>();
//Create an array that will hold tab-del values
String temp[] = new String[13];
//Define the file that will be used for creating objects
String fileLocation = "theHerd.txt";
File textFile = new File(fileLocation);
//Populate the giraffes ArrayList with items from the text file
int i = 0;
while(i < 90) {
if (textFile.canRead()) {
Scanner in = new Scanner(textFile);
in = new Scanner(textFile);
while (in.hasNextLine()) {
//Split each line at every tab (13 times), store in array temp
temp = in.nextLine().split("\\t", 13);
//Create a new giraffe from the array of strings in temp
giraffes.add(new Giraffe2(temp));
i++;
}
in.close();
}
//Print out ArrayList of giraffes
System.out.println(giraffes);
//Convert ArrayList to array
Object[] giraffes2 = giraffes.toArray();
//Print out a few examples
System.out.println(giraffes2[0]);
System.out.println(giraffes2[1]);
System.out.println(giraffes2[89]);
}
}
}
The output of this code is:
[giraffe.Giraffe2@55f96302, giraffe.Giraffe2@3d4eac69, giraffe.Giraffe2@42a57993, giraffe.Giraffe2@75b84c92, giraffe.Giraffe2@6bc7c054, giraffe.Giraffe2@232204a1, giraffe.Giraffe2@4aa298b7, giraffe.Giraffe2@7d4991ad, giraffe.Giraffe2@28d93b30, giraffe.Giraffe2@1b6d3586, giraffe.Giraffe2@4554617c, giraffe.Giraffe2@74a14482, giraffe.Giraffe2@1540e19d, giraffe.Giraffe2@677327b6, giraffe.Giraffe2@14ae5a5, giraffe.Giraffe2@7f31245a, giraffe.Giraffe2@6d6f6e28, giraffe.Giraffe2@135fbaa4, giraffe.Giraffe2@45ee12a7, giraffe.Giraffe2@330bedb4, giraffe.Giraffe2@2503dbd3, giraffe.Giraffe2@4b67cf4d, giraffe.Giraffe2@7ea987ac, giraffe.Giraffe2@12a3a380, giraffe.Giraffe2@29453f44, giraffe.Giraffe2@5cad8086, giraffe.Giraffe2@6e0be858, giraffe.Giraffe2@61bbe9ba, giraffe.Giraffe2@610455d6, giraffe.Giraffe2@511d50c0, giraffe.Giraffe2@60e53b93, giraffe.Giraffe2@5e2de80c, giraffe.Giraffe2@1d44bcfa, giraffe.Giraffe2@266474c2, giraffe.Giraffe2@6f94fa3e, giraffe.Giraffe2@5e481248, giraffe.Giraffe2@66d3c617, giraffe.Giraffe2@63947c6b, giraffe.Giraffe2@2b193f2d, giraffe.Giraffe2@355da254, giraffe.Giraffe2@4dc63996, giraffe.Giraffe2@d716361, giraffe.Giraffe2@6ff3c5b5, giraffe.Giraffe2@3764951d, giraffe.Giraffe2@4b1210ee, giraffe.Giraffe2@4d7e1886, giraffe.Giraffe2@3cd1a2f1, giraffe.Giraffe2@2f0e140b, giraffe.Giraffe2@7440e464, giraffe.Giraffe2@49476842, giraffe.Giraffe2@78308db1, giraffe.Giraffe2@27c170f0, giraffe.Giraffe2@5451c3a8, giraffe.Giraffe2@2626b418, giraffe.Giraffe2@5a07e868, giraffe.Giraffe2@76ed5528, giraffe.Giraffe2@2c7b84de, giraffe.Giraffe2@3fee733d, giraffe.Giraffe2@5acf9800, giraffe.Giraffe2@4617c264, giraffe.Giraffe2@36baf30c, giraffe.Giraffe2@7a81197d, giraffe.Giraffe2@5ca881b5, giraffe.Giraffe2@24d46ca6, giraffe.Giraffe2@4517d9a3, giraffe.Giraffe2@372f7a8d, giraffe.Giraffe2@2f92e0f4, giraffe.Giraffe2@28a418fc, giraffe.Giraffe2@5305068a, giraffe.Giraffe2@1f32e575, giraffe.Giraffe2@279f2327, giraffe.Giraffe2@2ff4acd0, giraffe.Giraffe2@54bedef2, giraffe.Giraffe2@5caf905d, giraffe.Giraffe2@27716f4, giraffe.Giraffe2@8efb846, giraffe.Giraffe2@2a84aee7, giraffe.Giraffe2@a09ee92, giraffe.Giraffe2@30f39991, giraffe.Giraffe2@452b3a41, giraffe.Giraffe2@4a574795, giraffe.Giraffe2@f6f4d33, giraffe.Giraffe2@23fc625e, giraffe.Giraffe2@3f99bd52, giraffe.Giraffe2@4f023edb, giraffe.Giraffe2@3a71f4dd, giraffe.Giraffe2@7adf9f5f, giraffe.Giraffe2@85ede7b, giraffe.Giraffe2@5674cd4d, giraffe.Giraffe2@63961c42]
giraffe.Giraffe2@55f96302
giraffe.Giraffe2@3d4eac69
giraffe.Giraffe2@63961c42
Any help is greatly appreciated. I have been fighting with this for a couple of days now and I'm having issues really understanding what the issue is
toString()in your Giraffe2 class