0

I have been playing around with reference variables and arrays. When I run my program, the first loop is perfect but the second and following loops of my array skip the first line of code from the constructor.

public class DogPark
{
 private Dog[] myDogs = new Dog[3];
 
 public DogPark() {
     for (int i = 0; i < myDogs.length; i ++){
        myDogs[i] = new Dog();
        }
 }

 public static void main(String[] args) {
    DogPark park = new DogPark();
    park.use();
    }
}

Here's my constructor:

public Dog()
    {
        name = readString("name");
        colour = readString("colour");
        age = readAge();
    }

private String readString(String type) {
        System.out.print("Enter your dog's " + type + ":");
        return In.nextLine();
    }
    
    private int readAge() {
        System.out.print("Enter your dog's age: ");
        return In.nextInt();
    }
    
    @Override
    public String toString() {
        return "My dog's name is " + name + ", it's colour is " + colour + " and is " + age + " years old."; 
    }
}

Thanks guys

0 Answers0