4

My program should print out the name and age of all cats with claws who are over 3 years old. For the following input

Enter the name of Cat 1: Sam
Enter the age of Cat 1: 1
Enter the weight of Cat 1: 5
Enter the breed of Cat 1: fluffy1
Does the cat have claws? True or False?: True

Enter the name of Cat 2: Tom 
Enter the age of Cat 2: 4
Enter the weight of Cat 2: 5
Enter the breed of Cat 2: fluffy2
Does the cat have claws? True or False?: True

Enter the name of Cat 3: Bob
Enter the age of Cat 3: 5
Enter the weight of Cat 3: 5
Enter the breed of Cat 3: fluffy3
Does the cat have claws? True or False?: False

The output shoud look like this.

The Cats over 3 with claws are:
Name: Tom
Age: 4 Years Old

However, the program finishes with no output. Here is the code I executed.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class MyClass {

    private static Cat[] catArray = new Cat[3];
    public static void main(String[] args) throws IOException {

        for (int i=0;i<3;i++) {
            Cat cat = new Cat();
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter the Name of Cat " + (i+1) + ": ");
            cat.setName(in.readLine());
            System.out.print("Enter the Age of Cat " + (i+1) + ": ");
            cat.setAge((Integer.parseInt(in.readLine())));
            System.out.print("Enter the Weight of Cat " + (i+1) + ": ");
            cat.setWeight((Double.parseDouble(in.readLine())));
            System.out.print("Enter the Breed of Cat " + (i+1) + ": ");
            cat.setBreed(in.readLine());
            System.out.print("Does the cat have claws? True or False: ");
            String hasClaws = in.readLine();
            if(hasClaws.equals("False"))
                cat.sethasClaws(false);
            else
                cat.sethasClaws(true);
            catArray[i] = cat;
        }
        for(int j=0;j<3;j++) {
            if((catArray[j].getAge()>=3) && (!catArray[j].hasClaws()) ) {
                System.out.println("The cats over 3 with claws are: \n");
                System.out.println("Name: " + catArray[j].getName());
                System.out.println("Age: " + catArray[j].getAge() + " years old");
            }
        }
    }
}

and also:

public class Cat {
    private String name;
    private int age;
    private double weight;
    private String breed;
    private boolean hasClaws;

    public Cat() {}

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public String getBreed() {
        return breed;
    }

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

    public boolean hasClaws() {
        return hasClaws;
    }

    public void sethasClaws(boolean hasClaws) {
        this.hasClaws = hasClaws;
    }
}
Undo
  • 25,519
  • 37
  • 106
  • 129
  • When you say it doesn't print out anything, do you mean you see literally no output (e.g. the `"Enter the Name of Cat 1"` message), or do you mean it doesn't print any cats aged 3 or above with claws? – Andy Turner Apr 05 '16 at 19:43
  • Please do not deface your question. – JAL Apr 05 '16 at 21:12
  • I'm going to remove your name from the question, but **don't continue editing it**. That will just make life hard for both of us. – Undo Apr 05 '16 at 21:36

1 Answers1

2

I believe that the ! operator in (catArray[j].getAge()>=3) && (!catArray[j].hasClaws()) is messing you up. This equates to "show me all cats 3 or over, that don't have claws".

from: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html

! Logical complement operator; inverts the value of a boolean

Try this:

for(int j=0;j<3;j++) {
    if((catArray[j].getAge()>=3) && (catArray[j].hasClaws()) ) {
        System.out.println("The cats over 3 with claws are: \n");
        System.out.println("Name: " + catArray[j].getName());
        System.out.println("Age: " + catArray[j].getAge() + " years old");
    }
}
Reti43
  • 9,656
  • 3
  • 28
  • 44
runningviolent
  • 317
  • 3
  • 13
  • 1
    Your point is true. But it doesn't seem to be what causes the problem. If that was the case, output was Bob. But I think he said it doesn't show anything. – Ali Seyedi Apr 05 '16 at 20:29
  • That's a good point. OP, Any change in output or is the issue still outstanding? – runningviolent Apr 05 '16 at 22:04