Results 1 to 2 of 2
Thread: Recursive Counting
 01292009, 08:12 PM #1Member
 Join Date
 Dec 2008
 Posts
 6
 Rep Power
 0
Recursive Counting
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.
Java Code:import java.util.*; public class testCopy { static int i=0; public static void main (String args[]) { int integer=0; int digit=0; Scanner keyboard = new Scanner (System.in); System.out.println("Please enter an integer:"); integer=keyboard.nextInt(); System.out.println("Please enter a single digit:"); digit=keyboard.nextInt(); System.out.println(); System.out.println("You have chosen integer "+integer+" and digit "+digit+"."); System.out.println(); System.out.println(frequencyCount(integer,digit)); } public static int frequencyCount(int n,int d) { if ((n==0) && (Integer.toString(n).length())==1) return(i); else if(n%10==d) { i++; return (frequencyCount(n/10,d) ); //if whatever is equal to d then return whats left plus one more } else return (frequencyCount(n/10,d)); } }
Any help would be greatly appreciated! Thanks!
 01292009, 09:42 PM #2Senior Member
 Join Date
 Sep 2008
 Posts
 564
 Rep Power
 13
Similar Threads

Counting numbers up and down
By radio in forum New To JavaReplies: 4Last Post: 05062011, 04:03 PM 
Counting characters
By Tiff89 in forum New To JavaReplies: 10Last Post: 12122008, 10:21 AM 
Need help with counting letters
By mrdestroy in forum New To JavaReplies: 15Last Post: 10222008, 02:33 PM 
Counting Pixels
By shaungoater in forum Java 2DReplies: 5Last Post: 11292007, 06:51 PM 
Counting Vowels and Constonants
By MattN in forum New To JavaReplies: 3Last Post: 11202007, 06:45 PM
Bookmarks