Results 1 to 2 of 2
  1. #1
    zlwilly is offline Member
    Join Date
    Dec 2008
    Rep Power

    Default 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.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)
            else if(n%10==d)
            return (frequencyCount(n/10,d) ); //if whatever is equal to d then return whats left plus one more
            return (frequencyCount(n/10,d));
    Is there some way to use the recursion to do the counting for me?
    Any help would be greatly appreciated! Thanks!

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Rep Power


    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:

    Java Code:
    if current digit matches
    then return recursive call + 1
    else return recursive call

Similar Threads

  1. Counting numbers up and down
    By radio in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 04:03 PM
  2. Counting characters
    By Tiff89 in forum New To Java
    Replies: 10
    Last Post: 12-12-2008, 10:21 AM
  3. Need help with counting letters
    By mrdestroy in forum New To Java
    Replies: 15
    Last Post: 10-22-2008, 02:33 PM
  4. Counting Pixels
    By shaungoater in forum Java 2D
    Replies: 5
    Last Post: 11-29-2007, 06:51 PM
  5. Counting Vowels and Constonants
    By MattN in forum New To Java
    Replies: 3
    Last Post: 11-20-2007, 06:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts