How to convert 100 into ONE ZERO ZERO not ONE?

Tags:



I have to call a method which is converting number to words and two others methods which are reversing and getting count of entered number and have to call both in first method but in case of number 100 it is giving output as ONE but it should give ONE ZERO ZERO.So my confusion is how to call count method in converting number method and to make give output as ONE ZERO ZERO with simple calling because I am using intelliJ and a beginner So I have to find out how to call get count method in first method which is number to word.

public static void numberToWords(int number) {
    if (number == 0) {
        System.out.println("Zero");
    } else if (number < 0) {
        System.out.println("Invalid Value");
    }

    int revInteger = reverse(number);
    while (revInteger != 0) {
        int modulus = revInteger % 10;
        revInteger /= 10;

        switch (modulus) {

            case 1:
                System.out.println("One");
                break;

            case 2:
                System.out.println("Two");
                break;

            case 3:
                System.out.println("Three");
                break;

            case 4:
                System.out.println("Four");
                break;

            case 5:
                System.out.println("Five");
                break;

            case 6:
                System.out.println("Six");
                break;

            case 7:
                System.out.println("Seven");
                break;

            case 8:
                System.out.println("Eight");
                break;

            case 9:
                System.out.println("Nine");
                break;

            default:
                break;
        }
    }
}


public static int reverse(int reversedNumber) {
    int revNumber = reversedNumber;
    int digit = 0;
    while (revNumber != 0) {
        int n = revNumber % 10;
        digit = digit * 10 + n;
        revNumber /= 10;
    }
    return digit;
}

public static int getDigitCount(int number) {
    int count = 0;
    if (number > 0) {
        while (number != 0) {
            number /= 10;
            ++count;
        }
        return count;
    } else if (number == 0) {
        return 1;
    }
    return -1;
}

Answer

The problem is that the reverse of the number 100 is 001, which is just 1. However, there’s a trick you can pull, where you append 1 (via number*10 + 1) to the number before reversing it, and then terminate when the number remaining is 1 (of course you could append any non-zero digit, but 1 is as good as any). For example, in the case of 200 you’d reverse 2001, which is 1002. When you’ve output Two Zero Zero you’re left with 1, so terminate.

int revInteger = reverse(number * 10 + 1);
while (revInteger != 1) {...}

However, you also have to handle the 0 case in your switch statement:

case 0:
  System.out.println("Zero");
  break;

With these two changes your code works.

int revInteger = reverse(number * 10 + 1);
while (revInteger != 1)
{
    int modulus = revInteger % 10;
    revInteger /= 10;

    switch (modulus)
    {
    case 0:
        System.out.println("Zero");
        break;
    case 1:
        System.out.println("One");
        break;

    // snipped cases 2-8

    case 9:
        System.out.println("Nine");
        break;

    default:
        break;
    }
}


Source: stackoverflow