In: Computer Science
in java please:
Given any integer, print an English phrase that describes the integer (e.g. “One Thousand, Two Hundred Thirty Four”). An ArrayList must be used in your program.
class PrintEng
{
static void convertNumberToEnglish(char[] num)
{
int len = num.length;
if (len == 0)
{
System.out.println("Sorry string is
Empty");
return;
}
if (len > 4)
{
System.out.println("String length
is greater than 4, it's not supported");
return;
}
String[] singleDigitNumber = new String[]{ "zero",
"one",
"two", "three", "four",
"five", "six", "seven",
"eight", "nine"};
String[] twoDigitsNumber = new String[]{"", "ten",
"eleven", "twelve",
"thirteen", "fourteen",
"fifteen", "sixteen", "seventeen",
"eighteen", "nineteen"};
String[] tensMultipleNumber = new String[]{"", "",
"twenty", "thirty", "forty",
"fifty","sixty",
"seventy",
"eighty", "ninety"};
String[] tensPwerNumber = new String[] {"hundred", "thousand"};
System.out.print(String.valueOf(num)+": ");
if (len == 1)
{
System.out.println(singleDigitNumber[num[0] - '0']);
return;
}
int x = 0;
while (x < num.length)
{
if (len >= 3)
{
if (num[x]-'0'
!= 0)
{
System.out.print(singleDigitNumber[num[x] -
'0']+" ");
System.out.print(tensPwerNumber[len - 3]+"
");
}
--len;
}
/* Code path for last 2 digits
*/
else
{
if (num[x] - '0'
== 1)
{
int sum = num[x] - '0' +
num[x] - '0';
System.out.println(twoDigitsNumber[sum]);
return;
}
else if (num[x]
- '0' == 2 && num[x + 1] - '0' == 0)
{
System.out.println("twenty");
return;
}
else
{
int i = (num[x] - '0');
if(i > 0)
System.out.print(tensMultipleNumber[i]+"
");
else
System.out.print("");
++x;
if (num[x] - '0' != 0)
System.out.println(singleDigitNumber[num[x] - '0']);
}
}
++x;
}
}
// Executable Code
public static void main(String[] args)
{
convertNumberToEnglish("9423".toCharArray());
convertNumberToEnglish("5123".toCharArray());
convertNumberToEnglish("829".toCharArray());
convertNumberToEnglish("889".toCharArray());
}
}