我的输出为Null,而不是使用字符串的默认值

时间:2019-04-05 06:40:18

标签: java null default

当输入超出指定值时,我有一个被设置为默认值的字符串,但它始终返回null而不是默认值。我缺少什么代码才能获得正确的输出?

我发现我的整数和双精度值正确使用了默认值,但我的字符串却没有。

//这是我前端的一个片段

import java.util.Scanner;
public class AnimalFrontEnd {
    public static final int ARRAY_SIZE = 10;
    Scanner keyboard = new Scanner(System.in) ;

    public static void main(String[] args) {
        boolean cont = true;
        int input = 0;
        int type = 0;
        AnimalCollection collection = new AnimalCollection(ARRAY_SIZE);
        Scanner keyboard = new Scanner(System.in) ;
        String rName = "";
        System.out.println("Welcome to the Cat and Dog Collection");
        while(cont) {
            System.out.println("1. Add a cat or dog \n2. Remove a cat or dog \n3. Quit \nEnter a selection");
            input = Integer.parseInt(keyboard.nextLine());
            switch(input) {
            case 1:
                System.out.println("Would you like to add \n1. A House Cat \n2. A Leopard \n3. A Domestic Dog \n4. A Wolf");
                type = Integer.parseInt(keyboard.nextLine());
                switch(type) {
                case 1:
                    HouseCat kitty = getHCat();
                    collection.addAnimal(kitty);
                    break;

//在我的前端进一步

private static HouseCat getHCat() {
        String name;
        double weight;
        String mood;
        String cType;

        Scanner keyboard = new Scanner(System.in) ;
        System.out.println("Enter the cat's name, weight, mood, and type");
        name = keyboard.nextLine();
        weight = Double.parseDouble(keyboard.nextLine());
        mood = keyboard.nextLine();
        cType = keyboard.nextLine();
        return new HouseCat(name, weight, mood, cType);
    }

//我的抽象类

public abstract class Animal {
    public String name;
    public double weight;

    Animal(){
        this.name = "Cuddles";
        this.weight = 0;
    }
    public Animal(String name, double weight) {
        this.setName(name);
        if(weight>0) {
            this.setWeight(weight);
        }
        else {
            System.out.println("Invalid weight");
        }
    }
    public String getAName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    public String toString() {
        return "Name: "+name+" Weight: "+weight;
    }
}

//动物的延伸

public class Cat extends Animal {
    public String mood;

    Cat(){
        this.mood = "Sleepy";
    }
    public Cat(String name, double weight, String mood) {
        super(name, weight);
        if(mood.equalsIgnoreCase("sleepy")||mood.equalsIgnoreCase("playful")||mood.equalsIgnoreCase("hungry")) {
            this.setMood(mood);
        }
        else {
            System.out.println("Invalid mood");
        }
    }
    public String getMood() {
        return mood;
    }
    public void setMood(String mood) {
        this.mood = mood;
    }
    public String toString() {
        return super.toString()+" Mood: "+mood;
    }
}

//从Cat扩展的类

public class HouseCat extends Cat {
    public String cType;

    HouseCat(){
        this.cType = "Short Hair";
    }
    public HouseCat(String name, double weight, String mood, String cType) {
        super(name, weight, mood);
        if(cType.equalsIgnoreCase("Short Hair")||cType.equalsIgnoreCase("Bombay")||cType.equalsIgnoreCase("Ragdoll")||cType.equalsIgnoreCase("Sphinx")||cType.equalsIgnoreCase("Scottish Fold")) {
            this.setCType(cType);
        }
        else {
            System.out.println("Invalid type");
        }
    }
    public String getCType(){
        return cType;
    }
    public void setCType(String cType) {
        this.cType = cType;
    }
    public String toString() {
        return super.toString()+" Type: "+cType;
    }
}


我希望出现:

Welcome to the Cat and Dog Collection
1. Add a cat or dog 
2. Remove a cat or dog 
3. Quit 
Enter a selection
1
Would you like to add 
1. A House Cat 
2. A Leopard 
3. A Domestic Dog 
4. A Wolf
1
Enter the cat's name, weight, mood, and type
Booph
23
unhappy
maine coon
Invalid mood
Invalid type
Collection: Name: Booph Weight: 23.0 Mood: Sleepy Type: Short Hair

实际显示的内容:

Welcome to the Cat and Dog Collection
1. Add a cat or dog 
2. Remove a cat or dog 
3. Quit 
Enter a selection
1
Would you like to add 
1. A House Cat 
2. A Leopard 
3. A Domestic Dog 
4. A Wolf
1
Enter the cat's name, weight, mood, and type
Booph
23
unhappy
maine coon
Invalid mood
Invalid type
Collection: Name: Booph Weight: 23.0 Mood: null Type: null

3 个答案:

答案 0 :(得分:2)

您的家猫有两个构造函数:

// one constructor
HouseCat(){
    this.cType = "Short Hair";
}

// another constructor
public HouseCat(String name, double weight, String mood, String cType) {
    super(name, weight, mood);
    if(cType.equalsIgnoreCase("Short Hair")||cType.equalsIgnoreCase("Bombay")||cType.equalsIgnoreCase("Ragdoll")||cType.equalsIgnoreCase("Sphinx")||cType.equalsIgnoreCase("Scottish Fold")) {
        this.setCType(cType);
    }
    else {
        System.out.println("Invalid type");
    }
}

您要在前端调用具有4个参数的构造函数,而不是无参数的构造函数,因此this.cType = "Short Hair";永远不会运行。

Cat中也发生了同样的事情-您正在使用3个参数调用构造函数,而不是将mood设置为默认值的无参数的构造函数。

要解决此问题,只需删除无参数构造函数,然后内联初始化变量即可:

// in HouseCat
public String cType = "Short Hair"; // btw you shouldn't use public fields.

// in Cat
public String mood = "Sleepy";

答案 1 :(得分:0)

创建HouseCat的对象时,您调用了参数化的Constructor,并在默认构造函数中初始化了TypeMood,这就是为什么它们为空的原因。

您需要在参数化的Constructor中设置这些值,然后它们将显示您期望的输出。像Housecat构造函数一样

public HouseCat(String name, double weight, String mood, String cType) {
        super(name, weight, mood);
        if(cType.equalsIgnoreCase("Short Hair")||cType.equalsIgnoreCase("Bombay")||cType.equalsIgnoreCase("Ragdoll")||cType.equalsIgnoreCase("Sphinx")||cType.equalsIgnoreCase("Scottish Fold")) {
            this.setCType(cType);
        }
        else {
            System.out.println("Invalid type");
            this.cType = "Short Hair";//<------------- set default value here
        }
    }

Cat的构造函数应该像

一样进行修改
public Cat(String name, double weight, String mood) {
        super(name, weight);
        if(mood.equalsIgnoreCase("sleepy")||mood.equalsIgnoreCase("playful")||mood.equalsIgnoreCase("hungry")) {
            this.setMood(mood);
        }
        else {
            System.out.println("Invalid mood");
           this.mood = "Sleepy";//<-------------  set default value here
        }
    } 

答案 2 :(得分:0)

您已经在Cat类中创建了两个构造函数:

Cat(){
        this.mood = "Sleepy";
    }

    public Cat(String name, double weight, String mood) {
        super(name, weight);
        if(mood.equalsIgnoreCase("sleepy")||mood.equalsIgnoreCase("playful")||mood.equalsIgnoreCase("hungry")) {
           this.setMood(mood);
        }
        else {
            System.out.println("Invalid mood");
        }
    }

只有第一个(没有参数)会初始化心情字段。而且您显然可以使用另一个实例来创建您的实例...

您在那里有多种解决方案: 1.删​​除未使用的构造函数,并在其他构造函数中初始化心情 2.将未使用的构造函数更改为“简单”方法,您将在构造函数中super之后立即调用 3. ...

示例:

public Cat(String name, double weight, String mood) {
  super(name, weight);
  this.mood = "Sleepy";

 if(mood.equalsIgnoreCase("sleepy")||mood.equalsIgnoreCase("playful")||mood.equalsIgnoreCase("hungry")) {
            this.setMood(mood);
        }
        else {
            System.out.println("Invalid mood");
        }
    }

HTH!

相关问题