Hi there
I have recently started with a course in
Java. So I am very much a beginner. We got an assignment and I am stuck at the last part. I would greatly appreciate any help on how to solve the program. I will post the challenge and my solution so far. The problem is that my the method Reverse doesn't work with the added 'for loop printing the leading zeroes in the right order. Also we haven't done arrays or any more advanced subjects yet. So I need to solve this with a for loop. I can get all the methods to work fine on their own. However when I try to add the leading zeroes in the right order it just doesn't work. Can someone please help.
Thank you very much in advance.
Lydia J van Vuuren
CHALLENGE:
Number To Words
Write a method called numberToWords with one int parameter named number.
The method should print out the passed number using words for the digits.
If the number is negative, print "Invalid Value".
To print the number as words, follow these steps:
Extract the last digit of the given number using the remainder operator.
Convert the value of the digit found in Step 1 into a
word. There are 10 possible values for that digit, those being 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Print the corresponding word for each digit, e.g. print "Zero" if the digit is 0, "One" if the digit is 1, and so on.
Remove the last digit from the number.
Repeat Steps 2 through 4 until the number is 0.
The logic above is correct, but in its current state, the words will be printed in reverse order. For example, if the number is 234, the logic above will produce the output "Four Three Two" instead of "Two Three Four". To overcome this problem, write a second method called reverse.
The method reverse should have one int parameter and return the reversed number (int). For example, if the number passed is 234, then the reversed number would be 432. The method reverse should also reverse negative numbers.
Use the method reverse within the method numberToWords in order to print the words in the correct order.
Another thing to keep in mind is any reversed number with leading zeroes (e.g. the reversed number for 100 is 001). The logic above for the method numberToWords will print "One", but that is incorrect. It should print "One Zero Zero". To solve this problem, write a third method called getDigitCount.
The method getDigitCount should have one int parameter called number and return the count of the digits in that number. If the number is negative, return -1 to indicate an invalid value.
For example, if the number has a value of 100, the method getDigitCount should return 3 since the number 100 has 3 digits (1, 0, 0).
HINT: Use a for loop to print zeroes after reversing the number. As seen in a previous example, 100 reversed becomes 1, but the method numberToWords should print "One Zero Zero". To get the number of zeroes, check the difference between the digit count from the original number and the reversed number.
NOTE: When printing words, each word can be in its own line. For example, numberToWords(123); can be:
One
Two
Three
They don't have to be separated by a space.
NOTE: The methods numberToWords, getDigitCount, reverse should be defined as public static like we have been doing so far in the course.
NOTE: In total, you have to write 3 methods.
NOTE: Do not add a main method to the solution code.
MY SOLUTION SO FAR
[code=java]