-3

I do not understand why my output is not what I expected, instead of showing the persons information, the output displays: examples.Examples@15db9742

Am I doing something wrong in my code?

package examples;

public class Examples {
    String name;
    int age;
    char gender;

    public Examples(String name, int age, char gender){
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public static void main(String[] args) {
        Examples[] person = new Examples[10];
        person[0] = new Examples("Doe",25,'m');
        System.out.println(person[0]);
    }

}
999
  • 11
  • 1
  • 1
  • 6

6 Answers6

2

Add a toString() method to your class:

public class Examples {
String name;
int age;
char gender;

public Examples(String name, int age, char gender){
    this.name = name;
    this.age = age;
    this.gender = gender;
}

@Override
public String toString() {
    StringBuilder result = new StringBuilder();
    result.append(this.name + " ");
    result.append(this.age + " ");
    result.append(this.gender + " ");
    return result.toString();
}

public static void main(String[] args) {
   Examples[] person = new Examples[10];
   person[0] = new Examples("Doe",25,'m');
   System.out.println(person[0]);
}

}
Xinzz
  • 2,242
  • 1
  • 13
  • 26
1

When you say

System.out.println(person[0]);

java doesn't automatically know what you want printed out. To tell it, you write a method in your Examples class called toString() which will return a string containing the info you want. Something like:

@Override
public String toString() {
    return "Name: " + name + 
           " Age: " + String.valueOf(this.age) + 
           " Gender: " + String.valueOf(this.gender); 
}
takendarkk
  • 3,347
  • 8
  • 25
  • 37
1

Java has no way of knowing what you want it to print. By default, the toString() method is called when you use System.out.println() with an object.

Your Examples class should have its own toString() method so you can decide what to print. The default toString() returns a representation of the object in memory.

For example, to print out the object's name:

package examples;
public class Examples {

    ...

    @Override
    public String toString() {
        return name;
    }

}
alomeli
  • 49
  • 9
0

Your output is right, when you print an object the method toString() of the object is called; by default it returns what you see (the class and a memory direction). Override the method toString() of the class to make him return a descriptive String. E.g.:

public class Examples {
   // The same ...
   public String toString(){
      return "My name is " + name + " and I have " + age + " years."
   }
   // The same ...
}

If you do that you will get a more descriptive String when calling toString() and so when printing an object of class Examples. New output is

My name is Dow and I have 25 years.
sergioFC
  • 5,926
  • 3
  • 40
  • 54
0

person is an array of type Examples, so by acessing person[0] you are telling it to print an Examples instance. Since the Examples class does not implement an toString() method it will call the parent Object.toString() method that produces the output you are seeing.

Add the following method to your Examples class

public String toString() {
    return "[name="+this.name+", age="+this.age+", gender="+this.gender+"]";
}
ulyssesrr
  • 71
  • 5
0

You have explicitly to create a method which outputs the persons data or override the toString() method to do the same thing:

public class Person
{
    String name;
    int age;
    char gender;

    public Person(String name, int age, char gender)
    {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    //Override the toString() method 
    //is a usual programming technique 
    //to output the contents of an object
    public String toString()
    {
        return "Name: " + this.name + "\nAge: " + this.age + "\nGender: "
                + this.gender;
    }


    //You can also write something like this
    public void showInfo()
    {
        System.out.printf("Persons Info:\n\nName: %s\nAge: %s\nGender: %s", this.name, this.age, this.gender);
    }

    public static void main(String[] args)
    {
        Person p = new Person("bad_alloc", 97, 'm');

        //System.out.println("Persons info:\n" + p.toString());
        //If you want directly to "output the object" you have to override the toString() method anyway:

        //System.out.println(p);//"Outputting the object", this is possible because I have overridden the toString() method

        p.showInfo();
    }
}
user14312287
  • 72
  • 2
  • 14