0

Hello StackOverflow Community,

I am having a problem with some output involving adding elements into an array. I had created a program in class and it ran properly but when I run the same program/code on my own computer, I get the following output (sometimes, different numbers/errors are generated):

"The toys:

[email protected]@b162d5"

To make it more clear, here is the code:

package toysdemo;



public class ToysDemo {
private float price;
private String name;

public float getPrice(){
    return price;
}

public void setPrice(float newPrice){
    price = newPrice;
}

public String getName() {
    return name;
}

public void setName(String newName) {
    name = newName;
}

public static void printToys(ToysDemo arrayOfToys[], int size) {
    //display content of array
    System.out.println("The toys: ");
    for (int i = 0; i < size; i++) {
        System.out.print(arrayOfToys[i]);
    }
    System.out.println();
}//print toys


public static void main(String[] args) {
    ToysDemo arrayOfToys[] = new ToysDemo[5];
    int numberOfToys = 0;
    // create two toys and save into array
    ToysDemo toy = new ToysDemo();
    toy.setPrice((float)111.99);
    toy.setName("Giant Squid");
    arrayOfToys[numberOfToys++] = toy;

    ToysDemo toy2 = new ToysDemo();
    toy2.setPrice((float)21.99);
    toy2.setName("small Squid");
    arrayOfToys[numberOfToys++] = toy2;


    //print toys into array
    printToys(arrayOfToys, numberOfToys); //the call
}
}

It is a real simple program but it's frustrating on how the correct output will not display.

I would really appreciate it if anyone can help me figure out this dilemma.

Thank you

0

4 Answers 4

2

Actually, you're printing the reference of the ToysDemo object. In order to make the System.out.println(arrayOfToys[i]) work, your ToysDemo class needs to override toString method.

Sample code:

public class ToysDemo {

    //class content...

    @Override
    public String toString() {
        return "My name is: " + name + " and my price is: " + String.format("%.2f", price);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I see. I didn't realize it was printing references. Thank You for suggesting the toString method. What do you think could be the explanation for it working before without the toString?
@Naan when you run the program, the JVM will allocate the objects in some place in the heap and give each a different address. Every time you run the program, you should see different addresses for your objects, explaining the old behavior. Maybe your class had the toString method already defined and you probably wiped it out or you were using a int[] or String[] array instead of ToysDemo[] array.
2

When you call System.out.print(someToy), it calls someToy.toString() and prints the result.
If you don't override toString(), you get the default Object.toString(), which prints the class name and memory address.

2 Comments

Thanks for the comment. I didn't have toString before and it displayed the output correctly. Do you know why that could be?
@Naan: Because you were printing something else (eg, getName())
1

You need to add the function toString to the ToysDemo class. For example:

@Override
public String toString()
{
   return "Name: "+name+"\tPrice: "+price;
}

Comments

1

you need to override the method toString() of the Object class. If you do not do so JVM will execute the base class method which by default prints the fully qualifiled name of the class i.e class name with the package name and the memory address where the object is being stored and that is how you got that output. Now when you system.out.print is called it will go to the overriden method and implement it.

For example:

Employee {
    private String name;
    private int age;

    public void setName(String name) { this.name = name; }
    public String getName() { return this.name; }
    public void setAge(int age) { this.age = age; }
    public int getAge() { return this.age = age; }

    @Override
    public String toString() {
        return "Name of the employee is " + name + " and age is " + age; 
    }

    public static void main(String args[]) {
        Employee e = new Employee();
        e.setName("Robert"); 
        e.setAge(20); 
        System.out.println(e); 
    } 
} 

1 Comment

Here is an example class Employee { private String name; private int age; public void setName(String name) { this.name = name; } public String getName() { return this.name; } public void setAge(int age) { this.age = age; } public int getAge() { return this.age = age; } public String toString() { return "Name of the employee is " + name + " and age is " + age; } public static void main(String args[]) { Employee e = new Employee(); e.setName("Robert"); e.setAge(20); System.out.println(e); } } o/p: Name of the employee is Robert and age is 20 Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.