1

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

5
  • 1
    You need to implement toString() in your Giraffe2 class Commented Mar 12, 2015 at 20:18
  • Related: stackoverflow.com/questions/28994299/… Commented Mar 12, 2015 at 20:22
  • Thanks! That helps tremendously Commented Mar 12, 2015 at 20:29
  • Show your appreciation with upvotes and accepted answers :-) Commented Mar 12, 2015 at 20:31
  • Can't upvote yet and wasn't able to accept the answer for a few minutes because it came in too quickly! You guys broke the system by being so fast lol. Commented Mar 13, 2015 at 20:07

2 Answers 2

2

In order to print an object with System.out.println you need to implement the toString() method the class you want to print.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer. I was about to lose my mind over this.
0

For proper printing of the content of class objects, the class containing the objects must implement method toString():

public String toString () { 

   return // here write the results as you want them to appear on the screen;    
}

You can print the objects with or without calling toString() method, i.e. you will have the same results regardless of whether you use System.out.print(objectName.toString) or System.out.print(objectName). All you need is to put as a parameter the name(s) of the object(s) of this class in the method System.out.print(String s) after implementing the above mentioned method toString(), For instance:

public class Birdwatcher {

// the Array List in this class contains as its objects the elements of class Bird

       private ArrayList<Bird> birds;

    // here you write your other code 
}
    ...........................................................................................     

public class Bird { 

// objects of this class will be saved as an Array List in class Birdwatcher 

     private String name;
     private String latinName;

     public Bird (String name, String latinName){
       this.name = name;
       this.latinName = latinName;
     }

   // here you write your other code 

      public String toString() {

        // this is the required method in order to avoid the printing errors 

           return name + ", " + latinName;
      }
}

    ................................................................................



public class MainClass{

    public static void main(String[] args) {

       Birdwatcher birdwatcher = new Birdwatcher();

       // here you write your other code

       for (Bird allBirds : birdwatcher.birds()) {

           System.out.println(allBirds);
         // or  System.out.println(allBirds.toString);
       }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.