1

Here is my code:

public static void numberToWords(int number){
    int lastDigit;

    if(number < 0){
        System.out.println("Invalid Value");
    }
    while(number != 0) {
        lastDigit = number % 10; // lastDigit = 3
        number = number / 10;
        reverse(number);

        if (lastDigit == 0) {
            System.out.println("ZERO");
        }
        if (lastDigit == 1) {
            System.out.println("ONE");
        }
        if (lastDigit == 2) {
            System.out.println("TWO");
        }
        if (lastDigit == 3) {
            System.out.println("THREE");
        }
        if (lastDigit == 4) {
            System.out.println("FOUR");
        }
        if (lastDigit == 5) {
            System.out.println("FIVE");
        }
        if (lastDigit == 6) {
            System.out.println("SIX");
        }
        if (lastDigit == 7) {
            System.out.println("SEVEN");
        }
        if (lastDigit == 8) {
            System.out.println("EIGHT");
        }
        if (lastDigit == 9) {
            System.out.println("NINE");
        }
    }
}

public static void reverse(int a){
    int finalDigit = 0;
    int reverse1 = 0;

    while(a > 0) {
        finalDigit = a % 10;
        reverse1 = reverse1 * 10 + finalDigit;
        a = a / 10;
    }
}

My digits are printed as strings as desired, but they come in reverse order.

I tried to add the reverse method into the conversion method but the results are always the same, can't figure out how to reverse it.

GhostCat
  • 137,827
  • 25
  • 176
  • 248

2 Answers2

0
  1. Drop all the if statements, use an Array to store digit to word associations (digit = array index, string = array value)
  2. Find each digit from right to left, construct a list of all the digit words
  3. Reverse it
  4. Do whatever you want with the list (i.e. join it into a string)
class NumTest {
    
    private static final String[] digits = {
        "ZERO", "ONE", "TWO", "THREE", "FOUR",
        "FIVE","SIX", "SEVEN", "EIGHT", "NINE"
    };
    
    private String numAsString(int num) {
        List<String> lDigs = new ArrayList<>();
        while (num > 0) {
            String dig = digits[num % 10];
            lDigs.add(dig);
            num /= 10;
        }
        Collections.reverse(lDigs);
        return lDigs.stream().collect(Collectors.joining(" "));
    }
    
    @Test
    public void testNumAsString() {
        Map<Integer, String> nums = new LinkedHashMap<>();
        //nums.put(0, "ZERO"); <- will return empty string
        nums.put(1, "ONE");
        nums.put(24, "TWO FOUR");
        nums.put(2567,  "TWO FIVE SIX SEVEN");
        nums.put(90000, "NINE ZERO ZERO ZERO ZERO");
        nums.put((int) 1.56e6, "ONE FIVE SIX ZERO ZERO ZERO ZERO");
        
        for (Entry<Integer, String> test: nums.entrySet()) {
            String res = numAsString(test.getKey());
            System.out.println(String.format("%d -> %s", test.getKey(), res));
            assertEquals(test.getValue(), res);
        }
    }
    
}
julien.giband
  • 2,467
  • 1
  • 12
  • 19
0

if you do use a condition lastdigit>10 than it is make simple program

Ramcr32
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 08 '22 at 05:12