Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-24-2010, 04:07 AM   PM User | #1
seabass
New to the CF scene

 
Join Date: May 2010
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
seabass is an unknown quantity at this point
Exclamation Help with java ASAP please

I have to make a program that takes an integer and outprint a string (eg. 247 = two hundred forty seven) for numbers up to 1,000,000,000.

ive gotten this so far need help.

[CODE]

import java.util.Scanner;

/**
This program turns an integer into its English name.
*/
public class IntegerName
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Please enter a positive integer < 1000000000: ");
int input = in.nextInt();
System.out.println(intName(input));
}

/**
Turns a number into its English name.
@param number a positive integer < 1,000
@return the name of the number (e.g. "two hundred seventy four")
*/
public static String intName(int number)
{
int part = number; // The part that still needs to be converted
String name = ""; // The name of the number

if(part >= 10000)
{
name = tensThousandName(part/10000) + " thousand";
part = part % 10000;
}

if(part >= 1000)
{
name = digitName(part/1000) + " thousand";
part = part % 1000;
}

if (part >= 100)
{
name = digitName(part / 100) + " hundred";
part = part % 100;
}

if (part >= 20)
{
name = name + " " + tensName(part);
part = part % 10;
}
if (part >= 10)
{
name = name + " " + teenName(part);
part = 0;
}

if (part > 0)
{
name = name + " " + digitName(part);
}

return name;
}

/**
Turns a digit into its English name.
@param digit an integer between 1 and 9
@return the name of digit ("one" ... "nine")
*/



public static String digitName(int digit)
{
if (digit == 1) { return "one"; }
if (digit == 2) { return "two"; }
if (digit == 3) { return "three"; }
if (digit == 4) { return "four"; }
if (digit == 5) { return "five"; }
if (digit == 6) { return "six"; }
if (digit == 7) { return "seven"; }
if (digit == 8) { return "eight"; }
if (digit == 9) { return "nine"; }
return "";
}

/**
Turns a number between 10 and 19 into its English name.
@param number an integer between 10 and 19
@return the name of the given number ("ten" ... "nineteen")
*/
public static String teenName(int number)
{
if (number == 10) { return "ten"; }
if (number == 11) { return "eleven"; }
if (number == 12) { return "twelve"; }
if (number == 13) { return "thirteen"; }
if (number == 14) { return "fourteen"; }
if (number == 15) { return "fifteen"; }
if (number == 16) { return "sixteen"; }
if (number == 17) { return "seventeen"; }
if (number == 18) { return "eighteen"; }
if (number == 19) { return "nineteen"; }
return "";
}

/**
Gives the name of the tens part of a number between 20 and 99.
@param number an integer between 20 and 99
@return the name of the tens part of the number ("twenty" ... "ninety")
*/
public static String tensName(int number)
{
if (number >= 90) { return "ninety"; }
if (number >= 80) { return "eighty"; }
if (number >= 70) { return "seventy"; }
if (number >= 60) { return "sixty"; }
if (number >= 50) { return "fifty"; }
if (number >= 40) { return "forty"; }
if (number >= 30) { return "thirty"; }
if (number >= 20) { return "twenty"; }
return "";
}


}

[CODE]

The recommended approach is to rename intName() and create a new intName() with the
same input parameters and return type as the old intName(). The new intName should call the
renamed old intName to convert numbers below 1000 to their word equivalent. Alternatively,
you may use a recursive call to intName(), if you understand recursion. What you definitely
should not do is use multiple copies of the code in intName().
seabass is offline   Reply With Quote
Old 09-24-2010, 08:51 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Ok, and what is your question?
This is clearly a homework assignment, and as such we cannot provide you with a code solution. If you can provide more specific questions, we can answer those.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 09-25-2010, 06:05 PM   PM User | #3
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,220
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
What is Java ASAP? Is that a new API?
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:46 AM.


Advertisement
Log in to turn off these ads.