This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 38107
    
  22
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: 19656
    
  18

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: 38107
    
  22
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: 11175
    
  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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Program to print numbers from 0 to 100 in words
 
Similar Threads
Converting Number to Word
Do while error, I'm going crazy
Help in writing a better program for Vampire numbers
Puzzled
How close am I in getting this program right?