File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Convert Number To Character String Representation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert Number To Character String Representation" Watch "Convert Number To Character String Representation" New topic

Convert Number To Character String Representation

chris hayden

Joined: Mar 13, 2003
Posts: 1
user will enter the number 44 and will be returned forty-four
any suggestions on which classes or methods to use?
Greg Charles

Joined: Oct 01, 2001
Posts: 2901

If the user enters it, it will already be in String form. Normally to convert an integer (say x) to a String, you can just use:
String s = "" + x;
and to convert a String to an integer:
x = Integer.parseInt(s);
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
He needs to turn:
32 -> thirty-two
75 -> seventy-five
It's not too complicated. You can use the String method toCharArray to convert the string to an array of chars. Then you need to work your way through the array.
A clever way to do it would be to have an array for each unit:
String[] tens = {"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}
Don't forget that most of the entries in the "ten" group need to be handled differently (thirteen not ten-three).

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
I agree. Here's the link:
subject: Convert Number To Character String Representation
It's not a secret anymore!