I have been given a task in which i have no idea on what to do, i have been writing my program and have reached this point where i cant carry on, the problem i have may be simple, for you some of you people but not really me, i need a way to list numbers from 1 to 1 million but in words so for example (One Two Three Four.......) i cant find a way of doing this other than setting each one individually which would take to long, alls i can imagine is that you would use a for loop and other than that i would just System.out.pritnln it to display it. can anyone help me with this?
3 Answers
I did something like this many years ago, so long don't even remember which language.
I think I used a Case statement for the one and two digit portions. Bit of a pain, but you only need to write the function once.
Basically you need your One through Nineteen, which are a bit of an oddity to parse. Then a single digit for your Twenty, Thirty up to Ninety. The rest is pretty easy, Hundred, Thousand, Million.
You would convert to string and get the length of the string and simply strip off the modulo 3 starting characters. For example 1234 is four characters, hence Thousands, parse off the first (4 % 3 = 1) character and decode in case statement. Then 234 would be Hundred, get the case for the first character (Two), leaving 34. First character
This is pre-caffine, not quite awake yet (LOL), so I hope this helps...

- 11
- 4
Here's another one for you, not the most elegant, but should give you a few more ideas...
public class WriteNum {
private static final String[] NUMBERS = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
private static final String[] TEN_NUMBERS = { "", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "nintey" };
private static final String[] TEEN_NUMBERS = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
"ninteen" };
public static void main(String[] args) {
for (int k = 0; k < 10000; k++) {
System.out.println(k + "=" + toWords(k));
}
}
private static String toWords(int i) {
StringBuilder buf = new StringBuilder();
int hundreds = i % 1000;
int hundredThousands = (i / 1000) % 1000;
if (hundredThousands == 0 && hundreds == 0) {
buf.append("Zero");
} else {
if (hundredThousands > 0) {
writeHundreds(buf, hundredThousands);
buf.append(" thousand");
if (hundreds > 0) {
buf.append(" ");
}
}
if (hundreds > 0) {
writeHundreds(buf, hundreds);
}
}
return buf.toString();
}
private static void writeHundreds(StringBuilder buf, int hundredThousands) {
int units = hundredThousands % 10;
int tens = (hundredThousands / 10) % 10;
int hundreds = (hundredThousands / 100);
if (hundreds > 0) {
buf.append(NUMBERS[hundreds]).append(" hundred");
}
boolean teen = false;
if (tens > 0) {
buf.append(" and ");
if (tens == 1) {
buf.append(TEEN_NUMBERS[units]);
teen = true;
} else {
buf.append(TEN_NUMBERS[tens]);
if (units > 0) {
buf.append(" ");
}
}
}
if (units > 0 && !teen) {
if (hundreds > 0 && tens == 0) {
buf.append(" and ");
}
buf.append(NUMBERS[units]);
}
}
}
Only does 0...999999, not as good as other solutions here!

- 4,141
- 13
- 22
You should divide the variable in hundreds tenths and units.
below some "quite working code" probably it needs some debug yet...
import java.util.HashMap;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
HashMap<Integer, String> numbers = new HashMap<Integer, String>();
numbers.put(1, "one");
numbers.put(2, "two");
numbers.put(3, "three");
numbers.put(4, "four");
numbers.put(5, "five");
numbers.put(6, "six");
numbers.put(7, "seven");
numbers.put(8, "eight");
numbers.put(9, "nine");
numbers.put(10, "ten");
numbers.put(11, "eleven");
numbers.put(12, "twelve");
numbers.put(13, "thirteen");
numbers.put(14, "fourteen");
numbers.put(15, "fifteen");
numbers.put(16, "sixteen");
numbers.put(17, "seventeen");
numbers.put(18, "eighteen");
numbers.put(19, "nineteen");
HashMap<Integer, String> tenths = new HashMap<Integer, String>();
tenths.put(1, "ten");
tenths.put(2, "twenty");
tenths.put(3, "thirty");
tenths.put(4, "forty");
tenths.put(5, "fifty");
tenths.put(6, "sixty");
tenths.put(7, "seventy");
tenths.put(8, "eighty");
tenths.put(9, "ninety");
for (int i=1; i <= 1000000; i++)
{
int million = i/1000000;
int hundred_thousand = i/100000;
int tenth_thousand = (i%100000)/10000;
int unit_thousand = ( tenth_thousand == 1 ? (i%20000)/1000 : (i%10000)/1000);
int hundred = i%1000/100;
int tenth = (i%100)/10;
int unit = (tenth == 1 ? i%20: i%10);
String output = "".concat((million > 0 ? numbers.get(million).concat(" million ") : ""))
.concat(hundred_thousand > 0 ? numbers.get(hundred_thousand).concat(" hundred ") : "")
.concat(tenth_thousand > 1 ? tenths.get(tenth_thousand).concat(" ") : "")
.concat(unit_thousand > 0 ? numbers.get(unit_thousand).concat(" ") : "")
.concat( i >= 1000 ? " thousand " : "")
.concat(hundred > 0 ? numbers.get(hundred).concat(" hundred ") : "")
.concat(tenth > 1 ? tenths.get(tenth).concat(" ") : "")
.concat(unit > 0 ? numbers.get(unit) : "");
System.out.println(output);
}
}
}

- 300
- 2
- 8