File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Program to print numbers from 0 to 100 in words Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Program to print numbers from 0 to 100 in words" Watch "Program to print numbers from 0 to 100 in words" New topic
Author

Program to print numbers from 0 to 100 in words

Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
Please give me inputs about this program. Like is this a good program, does it follow all conventions. Can this program be done in less number of steps etc.
I am a novice in programming and is doing this so that i can become good in programming.




Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38840
    
  23
Lots of people are given that exercise. I would suggest you divide your array into several arrays, putting "eleven" "twelve" . . . "nineteen" in one, and "ten" "twenty" . . . "ninety" in another. Using "" or even "\b" for the 0th element in each may make counting easier.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

You'll also want to use another array for the factors of 10. Forty is not four + ty, and eighty is not eight + ty


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38840
    
  23
Rob Prime wrote:You'll also want to use another array for the factors of 10. Forty is not four + ty, and eighty is not eight + ty
When my daughter taught English in Indonesia she pointed out how many people said "threety" for "thirty". She thought it endearing, but still had to take a mark off for it.
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
Hi,

Thanks for all the inputs. But my first doubt is why are we separating into different arrays. What added advantage would it give. I see that the complexity of an array for getting things at an index is O(1) so how will it make it more efficient.

Thanks for the replies once again.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Jacob Sonia wrote:
Thanks for all the inputs. But my first doubt is why are we separating into different arrays. What added advantage would it give. I see that the complexity of an array for getting things at an index is O(1) so how will it make it more efficient.

Efficiency is not the crux of this current problem (though this is not to say it never is the main issue), simplicity, correctness, and understandability are the issues here, and using separate arrays to keep like with like will make your program much simpler to implement, to upgrade, and to have others understand.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11313
    
  16

Readability is probably the single most important thing to consider when reading code. if I saw this:



I would have NO CLUE what that meant. I'd have to refer back to your array each time, and count over to find out what the 23rd element is.

However, this:



makes more sense - especially after seeing a string array declared as such:

static String[] tens = { "", "teen", "twenty", "Thirty", "Forty"...}


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Program to print numbers from 0 to 100 in words