Click to See Complete Forum and Search --> : convert problem


Loraine
11-30-2006, 12:57 AM
hi can you help how to convert the 3 digit numbers from a word (example) 133 i need this to convert into a word which is one hundred and thrty three ...

i have a code but its tooo long

if (nDig == 100)
{
System.out.prontln("one hundred");
}
if (nDig == 101)
{
System.out.println("one hundred one")
}

and so on

chazzy
11-30-2006, 06:39 AM
you'll probably want to analyze each digit individually, so using a substring of it, check each character.

if the first is "3" then print out "three hundred"
if the second is "5" print out "fifty"
if the third is "0" then print out nothing.

you can use a char[] and switch statements to handle this the best.

Loraine
11-30-2006, 08:43 AM
hi mr. chazzy .... thank you for advice but ... can you give me some example how to code it coz im new in java

aniseed
11-30-2006, 03:30 PM
hi mr. chazzy .... thank you for advice but ... can you give me some example how to code it coz im new in java

Here's one way of doing what you want. Obviously it has some limtations but you could start with thinking with this for a reference.

public class Test {
public static void main(String[] args) {
int number = 159;

/* Read input */

/* Add all checks to input */

System.out.println(convert2Word(number));
}

/* Convert number to Word */
public static String convert2Word(int number) {
int digit = 0; // Temp initialization - probably not needed
int decimalPlace = 0; // Temp initialization - probably not needed
String firstString = "";
String secondString = "";
String thirdString = "";

/* Read each digit and the position */
do {
// Get each digit.
digit = number%10;

// Divide the number by 10 to remove the considered digit.
number = number/10;

// Increment the decimal place.
decimalPlace++;

if(decimalPlace == 1) {
firstString = getUnitsPlace(digit);
} else if(decimalPlace == 2) {
secondString = getTensPlace(digit);
} else if(decimalPlace == 3) {
thirdString = getHundredsPlace(digit);
} else {
// No more than 3 digits implemented.
}

} while(number > 0);

return thirdString +" " +secondString +" " +firstString;
}

/* Get the String for units place */
private static String getUnitsPlace(int digit) {
String ret = "";
switch(digit) {
case 1:
ret = "One";
break;
case 2:
ret = "Two";
break;
case 3:
ret = "Three";
break;
case 4:
ret = "Four";
break;
case 5:
ret = "Five";
break;
case 6:
ret = "Six";
break;
case 7:
ret = "Seven";
break;
case 8:
ret = "Eight";
break;
case 9:
ret = "Nine";
break;
default:
ret = "";
}

return ret;
}

private static String getTensPlace(int digit) {
/* Write implementation here and remove hard-coding */
return "Fifty";
}

private static String getHundredsPlace(int digit) {
/* Write implementation here and remove hard-coding */
return "One Hundred";
}
}

agent_x91
12-01-2006, 08:42 AM
Aniseed, rather than using a switch statement to decide what to return, it would probably be a lot easier to use a static array:


static String[] words1={"","one","two","three","four","five","six","seven","eight","nine"};

return words1[num];

aniseed
12-04-2006, 04:22 PM
Yes it would but I wasn't posting the best code. I was only getting him/her started.

agent_x91
12-05-2006, 05:01 AM
Just making a suggestion. A switch statement is a very bulky way of doing it.