#Array returns memory address

20 messages · Page 1 of 1 (latest)

limber silo
#

The method getCarpetsWith(minimalRequiredArea) returns memory address (and method Arrays.ToString doesnt help too) however I need to get the array list as text. Btw the method printInfo needs to stay void - pretty new to java so hopefully someone helps, thank u!

    public static void main(String[] args) {

        CarpetShop CarpetShop = new CarpetShop();
        CarpetShop.createNewCarpetInShop("white", 5, 5);
        CarpetShop.createNewCarpetInShop("blue", 12, 5);
        CarpetShop.createNewCarpetInShop("", 5, 5);

        System.out.println((CarpetShop.getCarpetsWith(10)));
    }}```

The method
    ```public Carpet[] getCarpetsWith(int minimalRequiredArea) {
        int carpetsWithAreaIndex = 0;
        Carpet[] carpetsWithArea = new Carpet[getCarpetsCount(minimalRequiredArea)];
        for (int i = 0; i < carpets.length; i++) {
            if (carpets[i] != null && carpets[i].getArea() >= minimalRequiredArea) {
                carpetsWithArea[carpetsWithAreaIndex++] = carpets[i];
            }
        }
        return carpetsWithArea;
    }
}```

printInfo method
```    public void printInfo() {
        System.out.println(color + " " + sizeWidth + " " + sizeHeight);
    }```
left sailBOT
#

This post has been reserved for your question.

Hey @limber silo! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

brittle hearth
limber silo
#

I don’t have a method for toString, i tried to use the method from java library

brittle hearth
limber silo
#

oh, for the Carpet class I use the printInfo() method

#

however I cant use carpetsWithArea.printInfo()

brittle hearth
# limber silo oh, for the Carpet class I use the printInfo() method

then rename your printInfo() method into toString(). toString() is a method used by every object in java. Arrays.toString() also relies on said method. If you rename the toString() method to something else the default toString() method will be used which will just return the memory address.

limber silo
#

'toString()' in 'Carpet' clashes with 'toString()' in 'java.lang.Object'; attempting to use incompatible return type

#

however

#

i tried this and works but thanks for the helpCarpet[] get = CarpetShop.getCarpetsWith(10); int q = 0; while (q< get.length) { get[q].printInfo(); q++; }

brittle hearth
#

alternatively you could do this:

@Override
public String toString() {
return color + " " + sizeWidth + " " + sizeHeight;
}
#

to use this method then do:

...
Arrays.toString(carpets);
limber silo
#

okay, ill try thanks

brittle hearth
#

oh wait is printInfo void ?

limber silo
#

yes

brittle hearth
#

if you want the same information as before you'll need to copy paste (as shown above)

#

the advantage of overriding toString is that every instance of carpet will print the representation that was defined in said method

#

so instead of

Carpet myInstance = new Carpet();
myInstance.printInfo();
//You can do
System.out.println(myInstance);