无法将对象添加到构造函数中

时间:2016-12-09 14:41:16

标签: java oop object arraylist constructor

对于学校项目,我们应该创建一个简单的程序,从用户那里获取有关Dogs(名称,品种,年龄和体重)的一些输入,并将它们放入ArrayList中。一切似乎都在工作,但为了测试程序,我想在我的方法setUp()中添加一些狗,这样你就可以测试这些功能,而不必每次都添加新的狗,这就是我迷失的地方!如果我在setUp()中编写构造函数(Dog Dog = new Dog(" Alex"," Pug",10,10.0),我会收到错误消息:

The value of the local variable Dog is not used

我试图将构造函数放在案例中" 1"同时,我没有得到任何错误,但是狗没有添加到arraylist,而我可以在程序中添加新的狗。我真的不知道接下来要做什么。需要将狗添加到Dog-constructor中进行分配(部分代码被裁剪为相关,不要担心导入或主要)。

class DogReg {
private ArrayList<Dog> allDogs = new ArrayList<Dog>();
private Scanner keyboard = new Scanner(System.in);

private void setUp() {
    System.out.print("Hi! Welcome to the Dog register! \n" + "Choose an option between 1-5\n");
    System.out.println("1. Register your dog");
    System.out.println("2. Increase age");
    System.out.println("3. List");
    System.out.println("4. Delete");
    System.out.println("5. Exit");
}

private void runCommandLoop() {

    // Initierar en while-loop som körs under tiden att willRun == true
    boolean willRun = true;
    while (willRun == true) {

        System.out.print("> ");

        // Skapar en variabel som konverterar input-sträng till lowerCase
        String command = keyboard.next();
        switch (command) {

        case "1":
            // Konstruerar en ny hund in i ArrayList
            Dog Dog = new Dog();
            // Sparar all input till ArrayList
            System.out.print("\nThe name of the dog: ");
            Dog.setName(keyboard.next());
            System.out.print("The breed of the dog: ");
            Dog.setBreed(keyboard.next());
            System.out.print("The age of the dog: ");
            int age = keyboard.nextInt();
            Dog.setAge(age);
            System.out.print("The weight of the dog: ");
            double weight = keyboard.nextDouble();
            Dog.setWeight(weight);
            allDogs.add(Dog);
            break;    

班级狗{

private String name;
private String breed;
private int age;
private double weight;

public Dog (String name, String breed, int age, double weight){
    this.name = name;
    this.breed = breed;
    this.age = age;
    this.weight = weight;
}

public String getName() {
    return name;
}

public String getBreed() {
    return breed;
}

public int getAge() {
    return age;
}

public double getWeight() {
    return weight;
}

public void setName(String name) {
    this.name = name;
}

public void setBreed(String breed) {
    this.breed = breed;
}

public void setAge(int age) {
    this.age = age;
}

如果我使用此代码,我可以在setUp()中添加一只狗,但我们不应该使用它:

Dog Dog = new Dog();
Dog.setName("Bosse");
Dog.setBreed("Mops");
int age = 10;
Dog.setAge(age);
double weight = 10;
Dog.setWeight(weight);
allDogs.add(Dog);

希望它足够清楚,我为语法和/或拼写道歉,英语不是我的第一语言。

1 个答案:

答案 0 :(得分:0)

将您的Dog变量命名为不同的东西。您将变量命名为与对象名称相同的内容,这会使编译器感到困惑。试试这个,例如:

Dog newDog = new Dog();
newDog.setName("Bosse");
newDog.setBreed("Mops");
int age = 10;
newDog.setAge(age);
double weight = 10;
newDog.setWeight(weight);
allDogs.add(newDog);