//TestCountDigitsRecursively.java

import java.util.Scanner;

public class TestCountDigitsRecursively
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\nThis program reads in a positive integer "
            + "and then computes\nand prints out the number of digits "
            + "it contains.\n");
        boolean finished = false;
        do
        {
            System.out.print("Enter a positive integer: ");
            int n = keyboard.nextInt();
            keyboard.nextLine();
            System.out.print("The number of digits in " + n
                + " is " + numberOfDigits(n) + ".\n\n");
            System.out.print("Do it again? (y/[n]) ");
        }
        while (keyboard.nextLine().equalsIgnoreCase("y"));
    }

    public static int numberOfDigits
    (
        int n
    )
    /**
    Compute the number of digits in a positive integer.
    @return The number of digits in the integer n.
    @param n The integer whose digits are to be counted.
    @post n has been initialized with a positive integer.
    @post No other side effects.
    */
    {
        if (n < 10)
            return 1;
        else
            return 1 + numberOfDigits(n / 10);
    }
}

