//TestCountDigitsIteratively.java

import java.util.Scanner;

public class TestCountDigitsIteratively
{
    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
    )
    {
        int digitCount = 0;
        while (n > 0)
        {
            n = n/10;
            ++digitCount;
        }
        return digitCount;
    }
}

