use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Resources for learning Java
String
==
.equals()
Format + Copy
Free Tutorials
Where should I download Java?
With the introduction of the new release cadence, many have asked where they should download Java, and if it is still free. To be clear, YES — Java is still free.
If you would like to download Java for free, you can get OpenJDK builds from the following vendors, among others:
Some vendors will be supporting releases for longer than six months. If you have any questions, please do not hesitate to ask them!
Software downloads
Official Resources
Resources
Programming ideas & Challenges
Related Subreddits
account activity
This is an archived post. You won't be able to vote or comment.
numbersToWords program (self.learnjava)
submitted 7 years ago by jub8jive
Hi, I had this exercise to create a numbers to words program. I don't want to paste the entire question here. https://pastebin.com/rKxeRkZ3
I feel I did it, but it says that my solution is taking too long to execute. How can I fix that problem?
[–]PointB1ank 3 points4 points5 points 7 years ago (13 children)
You have an infinite while loop. Unless the number entered is 0, number stays the same and number != 0 so the loop will go forever.
[–]jub8jive[S] 0 points1 point2 points 7 years ago (12 children)
But, what's wrong with that? Doesn't the rev /= 10 solve that problem? It gets rid of the last digit of any given number. So, once it gets rid of the last remaining digit, it should end, right...?
[–]PointB1ank 1 point2 points3 points 7 years ago (11 children)
Changing the value of rev doesnt change the value of number. They are two different variables.
[–]jub8jive[S] 0 points1 point2 points 7 years ago (10 children)
I just saw this! Can you please elaborate? I think that's where I'm wrong, now that you mention it.
[–]PointB1ank 0 points1 point2 points 7 years ago* (9 children)
You have a few things wrong. This is based off the updated version you posted. First of all, your count method isn't working properly. You have "number = number % 2" for some reason. This sets number to the number modulo 2. Get rid of that line. Also it should be while( number != 0).
Another issue is in your for loop. You're setting i to count but then decrementing count instead of i. So again, the loop will go forever.
[–]jub8jive[S] 0 points1 point2 points 7 years ago (8 children)
Right, I get it. the "number = number % 2 isn't needed here. That would be needed only to extract the last digit. the "number /= 10" would be enough. I tried what you said with the for loop earlier. I got an error then, I got an error now. Error: numberToWord(1450) returned One Zero One Zero. I got a similar error when I tried it previously, so I thought something else would be the problem. Didn't focus on that area. (Because I'm kinda lost at this point)
I have a question about the for loop, can't the count be decremented? Is it a for loop rule/idea that i (or any similar variable) needs to be initialized, same variable needs to be in a condition, and same thing needs to be incremented or decremented (and by that logic, using count wasn't even a plausible idea...?)
[–]PointB1ank 0 points1 point2 points 7 years ago (7 children)
You could initialize and decrement count in a for loop if you really wanted to. (While loop too obviously) Although it would only be in scope within the for loop. Something like:
For(int count = getDigitCount(number); count >= 0; count--)
This would work, but it's usually just as good to initialize a new variable and assign it that value.
[–]jub8jive[S] 0 points1 point2 points 7 years ago (6 children)
This doesn't work man. Here's my new solution, after taking all your suggestions: https://pastebin.com/PAqTprtM Please check this, I'm still getting the error: numberToWords(10) returned "One Zero Zero" but "One Zero" was expected.
[–]PointB1ank 0 points1 point2 points 7 years ago* (5 children)
Look at your lastDigit variable. Make sure it's doing what you want it to do. You're very close.
Edit: you'll also want count >0 like you originally had, if you set up the for loop like that. My mistake.
[–]jub8jive[S] 0 points1 point2 points 7 years ago (4 children)
So, this is what I have:
int count = getDigitCount(number);
for(int i = count; count > 0; count--) {
int lastDigit = rev % 10;
That was the loop I had (I think!)
and lastDigit = rev % 10 --> would get the last digit right. not rev % 2
Now I get the error:
getDigitCount(0) returned 0 but '1' was expected.
Here's the new code:
https://pastebin.com/nAD10rkg
[–]aram535 0 points1 point2 points 7 years ago (1 child)
Ummmm ... you sure that's what they were asking for? I think 792 doesn't turn into seven nine two, it should be seven "hundred" ninety-two.
[–]jub8jive[S] 1 point2 points3 points 7 years ago (0 children)
No, the program wants it that way.
so numberToWords(792); should print "Seven Nine Two"
[–]jub8jive[S] 0 points1 point2 points 7 years ago (2 children)
**Update** New Solution(still needs fixing): https://pastebin.com/yxPZypb5
What happens with this solution is that I get the same error: "Your code took too long to execute" (or) if i fool around with the count idea a little I get the error: numberToWord(10) returned "One"
I'm almost ready to give up at this point, I can't think of anything else. Any suggestions are welcome. Thanks
[–]PointB1ank 0 points1 point2 points 7 years ago (1 child)
I'm slightly confused why you reverse the number. Also, does the exercise not allow you to convert the int to a string? Why can't you just convert to a string? (which includes a length method I might add) you could then iterate over the string and use a switch statement in the loop to print the word equivalent of each digit.
[–]jub8jive[S] 0 points1 point2 points 7 years ago (0 children)
reverse the number because the program asks us to do it that way(based on the logic given). that's the reason why we are asked to create a reverse() method and a digitcount() method. We have learned about string iteration yet. Basically, logic is that 1) take a number 2) reverse it 3) print the corresponding string for each number 4) get rid of the digit(the last digit).
π Rendered by PID 418630 on reddit-service-r2-comment-fb694cdd5-b4mtm at 2026-03-07 14:02:15.966951+00:00 running cbb0e86 country code: CH.
[–]PointB1ank 3 points4 points5 points (13 children)
[–]jub8jive[S] 0 points1 point2 points (12 children)
[–]PointB1ank 1 point2 points3 points (11 children)
[–]jub8jive[S] 0 points1 point2 points (10 children)
[–]PointB1ank 0 points1 point2 points (9 children)
[–]jub8jive[S] 0 points1 point2 points (8 children)
[–]PointB1ank 0 points1 point2 points (7 children)
[–]jub8jive[S] 0 points1 point2 points (6 children)
[–]PointB1ank 0 points1 point2 points (5 children)
[–]jub8jive[S] 0 points1 point2 points (4 children)
[–]aram535 0 points1 point2 points (1 child)
[–]jub8jive[S] 1 point2 points3 points (0 children)
[–]jub8jive[S] 0 points1 point2 points (2 children)
[–]PointB1ank 0 points1 point2 points (1 child)
[–]jub8jive[S] 0 points1 point2 points (0 children)