0

trying to use the count method but its not working

can someone help me figure out why my count method is not printing to total amount of streetcatss? public class Catss { String name; int age; String color; // to count how many street cats there are static int count = 0; //House cats public Catss(String name, int age, String color) { this.name = name; this.age = age; this.color= color; } //Street cats with no names public Catss() { count++; this.name = "Street cat No. " + count; Catss streetCat1 = new Catss(); count++; Catss streetCat2 = new Catss(); count++; Catss streetCat3 = new Catss(); count++; } public static void main(String[] args) { Catss apple = new Catss("Apple", 1, "Brown"); Catss banana = new Catss("Banana", 2, "White"); Catss carrot = new Catss("Carrots", 3, "Green"); Catss orange = new Catss("Orange", 4, "gray"); Catss grape = new Catss("Grape", 5, "Black"); System.out.println(apple.name+" "+apple.age+" "+apple.color); System.out.println(banana.name+" "+banana.age+" "+banana.color); System.out.println(carrot.name+" "+carrot.age+" "+carrot.color); System.out.println(orange.name+" "+orange.age+" "+orange.color); System.out.println(grape.name+" "+grape.age+" "+grape.color); System.out.println("Total number of street cats =" +count); System.out.println("\"Street cat No. \" + count;"); } }

5th Apr 2020, 10:32 PM
Pierre
1 Answer
+ 1
You should copy your code to Playground and share the link in your question description. It's easier to debug that way. Don't instanciate new street cats in your street cats method. That's infinite recursion and code won't work. public Catss() { count++; this.name = "Street cat No. " + count; } In your main method you have not created street cats yet, take it into account. If you want to keep track of both house and street cats you'd need two variables.
5th Apr 2020, 10:59 PM
Kevin â˜