Search code examples
javanulldefault

My output comes out Null instead of using the default value for strings


I have a string that is being set to a default when the input is outside of the specified values but it keeps returning null instead of the default value. what code am I missing to have the correct output?

I find that my integer and double values use the default correctly but my strings don't.

//This is a clip from my front end

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;

//Further down in my front end

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);
    }

//my abstract class

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;
    }
}

//an extension of animal

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;
    }
}

//the class that extends from 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;
    }
}


That I expect to show up:

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

What actually shows up:

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

Solution

  • Your house cat has two constructors:

    // 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");
        }
    }
    

    You are calling the constructor with 4 parameters in your front end, not the parameterless constructor, so this.cType = "Short Hair"; never got run.

    The same thing happens in Cat - you are calling the constructor with 3 parameters, not the parameterless one that sets mood to a default value.

    To fix this, just remove the parameterless constructors and instead initialise the variables inline:

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