Created
February 3, 2014 07:34
-
-
Save andrewmd5/8780059 to your computer and use it in GitHub Desktop.
Translate an standard arabic integer into an english word
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.util.logging.Logger; | |
public class IntegerToEnglish { | |
private static final String[] LARGE = { "", "", " Hundred", " Thousand", | |
" Million", " Billion" }; | |
private static String length; | |
private static final Logger logger = Logger | |
.getLogger(IntegerToEnglish.class.getName()); | |
private static int number; | |
private static final String[] SINGLES = { "Zero", " One", " Two", " Three", | |
" Four", " Five", " Six", " Seven", " Eight", " Nine" }; | |
private static final String[] TEENS = { " Ten", " Eleven", " Twelve", | |
" Thirteen", " Fourteen", " Fifteen", " Sixteen", " Seventeen", | |
" Eighteen", " Nineteen" }; | |
private static final String[] TENS = { " Twenty", " Thirty", " Fourty", | |
" Fifty", " Sixty", " Seventy", " Eighty", " Ninety" }; | |
public static final String convertToEnglish(final int number) { | |
length = intToString(number); | |
String convertedNumber = ""; | |
int position = 1; | |
boolean hung = false; | |
while (length.length() > 0) { | |
if (position == 1) { | |
if (length.length() >= 2) { | |
final String character = length.substring( | |
length.length() - 2, length.length()); | |
length = length.substring(0, length.length() - 2); | |
convertedNumber += getDigits(character); | |
} else if (length.length() == 1) { | |
convertedNumber += getDigits(length); | |
length = ""; | |
} | |
position++; | |
} else if (position == 2) { | |
final String C = length.substring(length.length() - 1, | |
length.length()); | |
length = length.substring(0, length.length() - 1); | |
if (convertedNumber.length() > 0 && getDigits(C) != "") { | |
convertedNumber = (getDigits(C) + LARGE[position] + " &") | |
+ convertedNumber; | |
hung = true; | |
} else { | |
if (getDigits(C) == "") { | |
; | |
} else { | |
convertedNumber = (getDigits(C) + LARGE[position]) | |
+ convertedNumber; | |
} | |
hung = true; | |
} | |
position++; | |
} else if (position > 2) { | |
if (length.length() >= 2) { | |
final String C = length.substring(length.length() - 2, | |
length.length()); | |
length = length.substring(0, length.length() - 2); | |
if (!hung && convertedNumber.length() > 0) { | |
convertedNumber = getDigits(C) + LARGE[position] | |
+ " and" + convertedNumber; | |
} else { | |
if (getDigits(C) == "") { | |
; | |
} else { | |
convertedNumber = getDigits(C) + LARGE[position] | |
+ convertedNumber; | |
} | |
} | |
} else if (length.length() == 1) { | |
if (!hung && convertedNumber.length() > 0) { | |
convertedNumber = getDigits(length) + LARGE[position] | |
+ " and" + convertedNumber; | |
} else { | |
if (getDigits(length) == "") { | |
; | |
} else { | |
convertedNumber = getDigits(length) | |
+ LARGE[position] + convertedNumber; | |
} | |
length = ""; | |
} | |
} | |
position++; | |
} | |
} | |
return convertedNumber; | |
} | |
private static final String getDigits(final String character) { | |
String converted = ""; | |
for (int i = character.length() - 1; i >= 0; i--) { | |
final int ch = character.charAt(i) - 48; | |
if (i == 0 && ch > 1 && character.length() > 1) { | |
converted = TENS[ch - 2] + converted; | |
} else if (i == 0 && ch == 1 && character.length() == 2) { | |
int sum = 0; | |
for (int j = 0; j < 2; j++) { | |
sum = (sum * 10) + (character.charAt(j) - 48); | |
} | |
return TEENS[sum - 10]; | |
} else { | |
if (ch > 0) { | |
converted = SINGLES[ch] + converted; | |
} | |
} | |
} | |
return converted; | |
} | |
private static final String intToString(int num) { | |
String number = ""; | |
while (num != 0) { | |
number = ((char) ((num % 10) + 48)) + number; | |
num /= 10; | |
} | |
return number; | |
} | |
public static void main(final String[] args) { | |
final Scanner input = new Scanner(System.in); | |
try { | |
logger.info("Enter Number to Convert : "); | |
number = input.nextInt(); | |
input.close(); | |
logger.info(convertToEnglish(number)); | |
} catch (final Exception e) { | |
logger.info(e.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment