Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can anyone help? Converting numbers to words?

Status
Not open for further replies.

wavo7

Technical User
Jul 18, 2001
11
US
Hey all,
Well, I'm stuck and I don't know what to do. I need to have this thing done by tomorrow, I feel like I am on the right track but can't quite finish it. I need to account for numbers all the way up to 1 billion. I have numbers 0 -99 taken care of. Does ANYONE have any suggestions or some help on my code to extend the functionality??
PLease.
Here's what I have:
import java.io.*;
import java.util.*;
import java.lang.*;


public class WordNumbers
{
public static void main(String[] args) throws IOException
{
final String quitString = "done";
String inputString;
int firstNumber, counter;

BufferedReader input =
new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter "+quitString+" when finished");

String numberWords[] = {"one","two","three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen",
"fourteen","fifteen","sixteen","seventeen","eighteen",
"nineteen"};
String tenWords[] = {"twenty","thirty","fourty","fifty","sixty","seventy",
"eighty","ninety"};
String bigWords[] = {"hundred","thousand","million","billion","trillion"};

inputString = input.readLine();

while (!inputString.equals(quitString))
{
firstNumber = Integer.parseInt(inputString);

if(firstNumber < 0)
{
System.out.println(&quot;negative &quot; + numberWords[firstNumber - 1]);
}

while (firstNumber > 0)
{
if(firstNumber >=1000000 && (firstNumber <=9999999))
{
System.out.println(bigWords[2]);
}
else
if(firstNumber >= 1000 && (firstNumber <= 9999))
{
System.out.println(bigWords[1]);
}
else
if(firstNumber > 99 && (firstNumber <= 999))
{
System.out.println(bigWords[0]);
}
else
if(firstNumber < 20)
{
System.out.println(&quot;You entered &quot; + numberWords[firstNumber - 1]);
}
else
{
int unitsNo = firstNumber % 10 ;
int decNo = firstNumber/10;
int unit2 = firstNumber % 100;
int dec2 = firstNumber/100;

if( unitsNo == 0 )
{
System.out.println(&quot;You entered &quot; + tenWords[ decNo - 2 ]);
}

if( unitsNo > 0)
{
System.out.println(&quot;You entered &quot; + tenWords[ decNo - 2 ] + &quot; &quot; + numberWords[ unitsNo - 1]);
}
else
if( unitsNo > 9)
{
System.out.println(&quot;hahah&quot;);
}
}
firstNumber = -1;
}

if(firstNumber == 0)
{
System.out.println(&quot;You entered zero&quot;);
}
inputString = input.readLine();
}
}
}
 
Try going to :)

This is a nice forum, but you'll get a good answer faster at Sun's forum :cool: &quot;and everything under the sun is in tune
but the sun is eclipsed by the moon.&quot; --Pink Floyd: Eclipse


&quot;I'm going to spend eternity
reinstalling Windows.&quot; --Reinstalling Windows: by some British guy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top