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