You can do it witout converting to string, by using the mod operator and truncating division, like this:
// non-recursive wrapper to handle degenerate case of 0,0 input.
publlc static int digitCount(int numberr, digit) {
if ((nunmber==0) && (digit == 0))
return 1;
else
return digitCountr(number, digit);
}
public static int digitCountr(int number, int digit){
if (number == 0)
return 0;
if (number % 10 == digit)
return 1 + digitCount(number / 10, digit);
else
return digitCount(number / 10, digit);
}
This will return 0 when called with the number arg equal to 0, which may not be what you want - but it relies on that actually in order to correctly count any actual zero digits so you'd need to put a wrapper around it if you want different behavior for that case.
public static int digitCount(int number, int digit) {
if ((number==0) && (digit == 0))
return 1;
else
return digitCountr(number, digit);
}
public static int digitCountr(int number, int digit){
if (number == 0)
return 0;
if (number % 10 == digit)
return 1 + digitCount(number / 10, digit);
else
return digitCount(number / 10, digit);
}
Other than that, though, I believe it is accurate.