I have an an exercise for class that I have been working on, trying to teach us recursion. I am able to nearly wrap my head around the subject, but have one small problem left. The program is supposed to take a large integer and a single digit integer, and tell how many times the small int occurs in the large one. This much I can get it to do.
However, it does the counting using a "static int i" that can be accessed by any part of the program, and I would prefer to use the recursion itself to accomplish this.
Here's the code.
Is there some way to use the recursion to do the counting for me?
public class testCopy
static int i=0;
public static void main (String args)
Scanner keyboard = new Scanner (System.in);
System.out.println("Please enter an integer:");
System.out.println("Please enter a single digit:");
System.out.println("You have chosen integer "+integer+" and digit "+digit+".");
public static int frequencyCount(int n,int d)
if ((n==0) && (Integer.toString(n).length())==1)
return (frequencyCount(n/10,d) ); //if whatever is equal to d then return whats left plus one more
Any help would be greatly appreciated! Thanks!
looks like you just need to get used to recursion. trying to make help as obscure as possible, here's some simple straightforward pseudocode to think on:
if current digit matches
then return recursive call + 1
else return recursive call