I'm trying to print every item in my array, but it gives me a value like PackOfCrisps@653f6b99. I have tried inputting toString() but that just tells me it cannot be converted to a string.
(PackOfCrisps is a separate class)
I'm really new to this
I'm trying to print every item in my array, but it gives me a value like PackOfCrisps@653f6b99. I have tried inputting toString() but that just tells me it cannot be converted to a string.
(PackOfCrisps is a separate class)
I'm really new to this
"PackOfCrisps@653f6b99" is the result of the default Object.toString() function. You need to Override the method in your PackOfCrisps class to actually return something useful.
class PackOfCrisps {
...
@Override
public String toString() {
return String.format("PackOfCrisps (flavor: %s)", flavor);
}
}
In toString()
you can return whatever you want, and also include whatever attributes your class has (like flavour for example).
Javadocs for Object.toString() in case you're interested: https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()