-1

I made a method that makes a list and fills it with "Dog" objects using FOR loop, and at the end prints the stuff that's in the list. However, when I run it, instead of printing the Strings it prints something weird like " list.Dog@eed1f14 "

public class Dog {
    private  String breed;
    private  String name;
    Scanner s = new Scanner(System.in);



  public void makeList(){
        System.out.println("What's ths size of the list?");
        int sizeOfList = s.nextInt();
        List<Dog> dog = new ArrayList<>();
        for (int i = 0; i < sizeOfList; i++) {
            Dog doggy = new Dog(breed, name);
            dog.add(doggy);
            System.out.println("What's the breed of the dog :");
            doggy.breed = s.next();

            System.out.println("What's the name of the dog:");
            doggy.name = s.next();

        }
        dog.forEach(System.out::println);

    }





public class Liste {


    public static void main(String[] args) {

        Dog p = new Dog();
        p.makeList();

    }

}

I expected it to return Name and Breed of the Object (Strings), instead it gave me "liste.Dog@eed1f14"

Dejvid
  • 11
  • 2

1 Answers1

1

That's the output of the toString method on Object. To get a more specific output for Dog, override toString in Dog:

// Inside the `Dog` class
@Override
public String toString() {
    // Create and return relevant string
}

(Note: The @Override annotation is optional, do what your team's guidelines say with regard to including it or not.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875