简体   繁体   中英

Java console displaying address of object rather than actual value

Ok so I've got a simple array that I'm working on in Java. Problem is, when I run the program I get the address of the object rather than the actual value. I also see to have aa problem with the loop/array. It should display houses 3, 5 and 7 but the bottom part is showing 3,4 and 5. Where am I going wrong? Please see code below, and console output. Thanks in advance!

House[] houses = new House[3];

houses[0] = new House(3,4);
houses[1] = new House(5,7);
houses[2] = new House(7,2);

System.out.println("Number of bottles in house number 3 is: " + houses[0]);
System.out.println("Number of bottles in house number 5 is: " + houses[1]);
System.out.println("Number of bottles in house number 7 is: " + houses[2]);

for (int i = 0; i < houses.length; i++){
  System.out.println("Number of bottles in house " + (i + 3 ) + " is " + houses[i]);
}

Console Output:

Number of bottles in house number 3 is: org.com1027.lab3.House@d16e5d6

Number of bottles in house number 5 is: org.com1027.lab3.House@5a4b4b50

Number of bottles in house number 7 is: org.com1027.lab3.House@53d9f80

Number of bottles in house 3 is org.com1027.lab3.House@d16e5d6

Number of bottles in house 4 is org.com1027.lab3.House@5a4b4b50

Number of bottles in house 5 is org.com1027.lab3.House@53d9f80

Java do not have any mechanism that magically know how your class should be represented in string format. That is why you must implement it by your own.

You need to override the toString() method to gain "corect" value

class House {
 //your code

 @Override 
 public String toString() {
   return "The string representation";
 }

}

When you print an instance like house[0] in your example you see the result of the toString method. If in your class House toString isn't overriden you see the default implementation which displays the classname and the object-ID of the instance. So if you want some custom string representation of your House instances you can override the toString() method in class House. There you can eg display the values of the member variables, etc.

your for loop iterates from 0 to 2 so your i assumes 0,1,2 and your System.out statement prints i+3 which results in 3,4,5

houses[0] is just a reference & it's not the object itself. Just points the object. So, write :

houses[0].getValues();

public String getValues() { return "" + x + "" + y }

or the proper method of house object.

In your statement, you concatenate a String with a House object. Thus, it will call toString() method of House and substitute its result.

Since you have probably not override this method, it uses default toString() method, which prints class name and object reference.

Simply override toString() method in your House class:

public class House {
   // ...

   @Override
   public String toString() {
       return "House: "; // Customize it!
   }

   // ...
}

What you want is to override toString() in your class House .

class House {
    // ... your implementation, 
    // which I'm guessing it's something like this:
    private int number;
    private int bottles;

    public House(int number, int bottles) {
        this.number = number;
        this.bottles = bottles;
    }

    // here you say what you want as output if you print a House, e.g.:
    @Override 
    public String toString() {
        return String.valueOf(bottles);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM