0

I am trying to print out an array of the object Teacher that I have created, and its giving me a weird output. [Teacher@659e0bfd, Teacher@2a139a55, Teacher@15db9742, Teacher@6d06d69c, Teacher@7852e922] I have had this problem before where I was trying to use toString() on a 2d array, but I am now using a 1d array of objects. I have tried using both deepToString() and toString(), but it still gives output like that. I tried looking up how to print out an array of Objects, but all I find is how to print out a regular array.

My Code:

import java.util.Arrays;
public class Main {
    public static void main(String[] args){
        Teacher[] teachers = new Teacher[5];
        Student[] students = new Student[25];


        createTeacherNames(teachers);

        System.out.println(Arrays.toString(teachers));
    }
    public static void createTeacherNames(Teacher[] teachers){
        teachers[0] = new Teacher("Mrs.", "Smith", 201);
        teachers[1] = new Teacher("Mr.", "Johnson", 202);
        teachers[2] = new Teacher("Mrs.", "Williams", 203);
        teachers[3] = new Teacher("Mr.", "Brown", 204);
        teachers[4] = new Teacher("Mr.", "Jones", 205);
    }
}

Person Class:

public class Person {
    public String firstName;
    public String lastName;

    public Person(String firstName, String lastName){
        this.setFirstName(firstName);
        this.setLastName(lastName);
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
}

Teacher Class:

public class Teacher extends Person {
    public int roomNumber;
    public Teacher(String firstName, String lastName, int roomNumber) {
        super(firstName, lastName);

    }

}

Student Class:

public class Student extends Person {
    public Student(String firstName, String lastName, int studentIdNumber, double GPA) {
        super(firstName, lastName);

    }

}

2 Answers2

3

In your Teacher class, override toString(), similar to this:

@Override 
public String toString() {
    String out = "";

    out = "[" + teacherPrefix + "," + firstName + "," + idNum + "]";

    return out;
}

Would return something like:

//[Mrs.,Smith,201]

It may be better to use a StringBuilder, but you can work out what you need from my example.

CubeJockey
  • 2,209
  • 8
  • 24
  • 31
2

You should create a custom toString() method (override the default toString() method of Teacher class). Teacher@659e0bfd is the object identifier and is returned by the default toString() method of an object.

It might look something like this:

public String toString()
{
    return super.getFirstName() + "   " super.getLastName() + "   " + this.roomNumber;
}
brso05
  • 13,142
  • 2
  • 21
  • 40
  • what is the syntax to do that? – Garrett Carder Apr 15 '15 at 13:28
  • 3
    @GarrettCarder Please.... go through a tutorial, this is the basics. – Maroun Apr 15 '15 at 13:30
  • @MarounMaroun I am going through a school programming class and they did not teach me this. – Garrett Carder Apr 15 '15 at 13:35
  • @GarrettCarder Googling "how to override toString in Java" will give you thousand of references. It's better for you to try writing code without getting a ready solution, this way you'll have a better learning curve. We're here for any question you might have. – Maroun Apr 15 '15 at 13:36
  • @Garrett, check the link that Maroun pasted to your submission, it may be helpful: http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 – CubeJockey Apr 15 '15 at 13:36
  • @brso05 it will not recognize the variables I used in my Teacher class do I need to put them in in toString(firstName, lastName, roomNumber) or do I need to do something else to put them in the main class – Garrett Carder Apr 16 '15 at 13:21
  • @GarrettCarder are they global variables in the `Teacher` class? Can you post your entire code for `Teacher` class? – brso05 Apr 16 '15 at 13:24
  • @brso05 I posted all 4 – Garrett Carder Apr 16 '15 at 13:32
  • @brso05 i have a Person class and I have a Teacher and Student class that inherit lastName and firstName from person then add more variables – Garrett Carder Apr 16 '15 at 13:33
  • @GarrettCarder you can call the methods of your super(Parent) class from the child class. Just like you call the constructor `super(firstName, lastName)` you can also call the getters and setters you have. In this case you want to call the getter for first and last name like: `super.getFirstName()`. Then you can directly reference the room number because it is declared in your child class `this.roomNumber`. I edited my post check it out hopefully that helps you... – brso05 Apr 16 '15 at 13:36