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!

HELP with extending functionality of this prog.

Status
Not open for further replies.

wavo7

Technical User
Jul 18, 2001
11
US
Hey all, I could really use your help. I'm super stuck. I'm writing a program that will except a number (positive or negative) from the user, and the display the number in written form to them. EX. 124 the computer will display one hundred twenty four. I need to account for negatives too. The problem is I have it working fine from 0 to 99, but I'm having a brain freeze on extending it to one billion. I need to account for numbers that high, it really shouldn't be too hard, but I'm having issues. Any help would be GREATLY appreciated, I need to finish this SOON. Thanks so much!

Here's the code I have so far:
This code should run on your machine if you have Java installed, sorry about the wrapping of the text, I hope you can make sense of it all.

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();
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top