1

I always get the following output, when I try to get an output from an Array:

test1.Buch@5ef4b65d 

Could someone please tell me what I did wrong?

Buch[] buch = new Buch[30]; 

for(int i = 1; i < buch.length; i++) {
    buch[i] = new Buch();

    char reply;
    Scanner in = new Scanner(System.in);

    // input data into the Array            
    //.....

    System.out.println(buch[i]);
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525

3 Answers3

5

You have to ovveride the toString() method in your class Buch.

As per docs of Object#toString()

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object.

So

test1.Buch@5ef4b65d is the textual representation of Buch class.

You just ovveride toString method in Buch class.

To get the required output override toString() like

class Buch{
    @Override
            public String toString() {
                 StringBuilder result = new StringBuilder();
                 result.append(this.name );
                 // append other properties too.
                 return result.toString();
            }
}

If you are simply printing a specific property of Buch class

System.out.println(buch[i].propertyname);

or

  System.out.println(buch[i].getProeprtyName());
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

You need to override toString method in your Buch clas to get the output in a pretty way.

When an object is printed using sysout, its toString method is called. If toString method is not overridden in the class, then its default implementation will be used. Default implementation of an object toString method prints the class name and the unsigned hexadecimal representation of the hash code of the object separated by @ symbol.

Note: Most of the IDEs provide a way to auto- generate the toString method.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

Override the toString method inherited from Object class. more info

Community
  • 1
  • 1
Isuru Gunawardana
  • 2,847
  • 6
  • 28
  • 60