
Andy C. answered 09/03/17
Tutor
4.9
(27)
Math/Physics Tutor
import javax.swing.*;
class AmountToText
{
private long amount; //stores the numerical amount
private String amtStr; //stores the text string representation of the amount
private long[] digits = new long[10]; //stores the digits of the number
AmountToText()
{
amount=0;
amtStr="";
for (int i=0; i<10; i++) { digits[i]=0; }
}
public String GetAmtStr() { return(amtStr); }
public long GetAmt() { return(amount); }
AmountToText( long amt) { amount = amt; }
public void DoConvert()
{
if (amount != 0)
{
long lAmt = amount; //copies the amount locally
long longIntNum = 1000000000; // 1 billion
for (int i=0; i<10; i++)
{
digits[i] = lAmt / longIntNum;
lAmt %= longIntNum;
longIntNum /= 10;
}
//debug-dumps the digits
for (int j=0; j<10; j++)
{
System.out.println(" j = " + j + " : digit is " + digits[j]);
}
DisplayResult();
}
else
{
amtStr = "zero";
}
} //DoConvert
private String GetTextDigit( long digit)
{
String returnStr=null;
switch ((int)digit)
{
case 1: { returnStr= " one "; break; }
case 2: { returnStr= " two "; break; }
case 3: { returnStr= " three "; break; }
case 4: { returnStr= " four "; break; }
case 5: { returnStr= " five "; break; }
case 6: { returnStr= " six "; break; }
case 7: { returnStr= " seven "; break; }
case 8: { returnStr= " eight "; break; }
case 9: { returnStr= " nine "; break; }
}
return(returnStr);
}
private String Get3DigitStr( long X)
{
String outBuff = " ";
long hundredsDigit = X / 100;
X %= 100;
long tensDigit = X / 10;
long onesDigit = X %= 10;
if (hundredsDigit>0 )
{
outBuff = GetTextDigit(hundredsDigit) + " hundred ";
}
if (tensDigit>1)
{
switch ((int)tensDigit)
{
case 2: { outBuff += " twenty "; break; }
case 3: { outBuff += " thirty "; break; }
case 4: { outBuff += " fourty "; break; }
case 5: { outBuff += " fifty "; break; }
case 6: { outBuff += " sixty "; break; }
case 7: { outBuff += " seventy "; break; }
case 8: { outBuff += " eighty "; break; }
case 9: { outBuff += " ninety "; break; }
}
}
else if (tensDigit==1)
{
switch ((int)onesDigit)
{
case 0: { outBuff += " ten "; break; }
case 1: { outBuff += " eleven "; break; }
case 2: { outBuff += " twelve "; break; }
case 3: { outBuff += " thirteen "; break; }
case 4: { outBuff += " fourteen "; break; }
case 5: { outBuff += " fifteen "; break; }
case 6: { outBuff += " sixteen "; break; }
case 7: { outBuff += " seventeen "; break; }
case 8: { outBuff += " eighteen "; break; }
case 9: { outBuff += " twenty "; break; }
}
}
if ((onesDigit>0) && (tensDigit != 1))
{
outBuff += GetTextDigit(onesDigit);
}
return(outBuff);
} //Get3DigitStr
private void DisplayResult()
{
amtStr = " ";
if (digits[0]>0)
{
amtStr = amtStr + GetTextDigit(digits[0]) + " billion ";
}
long digits3 = digits[1]*100+digits[2]*10+digits[3];
if (digits3>0)
{
amtStr = amtStr + Get3DigitStr(digits3) + " million ";
}
digits3 = digits[4]*100 + digits[5]*10 + digits[6];
if (digits3>0)
{
amtStr = amtStr + Get3DigitStr(digits3) + " thousand ";
}
digits3 = digits[7]*100+digits[8]*10+digits[9];
if (digits3>0)
{
amtStr = amtStr + Get3DigitStr(digits3);
}
System.out.println(amtStr);
} //DisplayResult
public static void main(String args[])
{
String inBuff;
long amt;
if (args.length>0)
{
inBuff = args[0];
}
else
{
inBuff=null;
while (inBuff== null)
{
inBuff = JOptionPane.showInputDialog(" Please input the number");
}
}
amt = Long.parseLong(inBuff);
AmountToText amt2Txt = new AmountToText(amt);
amt2Txt.DoConvert();
System.out.println(amt2Txt.GetAmtStr());
}
}
class AmountToText
{
private long amount; //stores the numerical amount
private String amtStr; //stores the text string representation of the amount
private long[] digits = new long[10]; //stores the digits of the number
AmountToText()
{
amount=0;
amtStr="";
for (int i=0; i<10; i++) { digits[i]=0; }
}
public String GetAmtStr() { return(amtStr); }
public long GetAmt() { return(amount); }
AmountToText( long amt) { amount = amt; }
public void DoConvert()
{
if (amount != 0)
{
long lAmt = amount; //copies the amount locally
long longIntNum = 1000000000; // 1 billion
for (int i=0; i<10; i++)
{
digits[i] = lAmt / longIntNum;
lAmt %= longIntNum;
longIntNum /= 10;
}
//debug-dumps the digits
for (int j=0; j<10; j++)
{
System.out.println(" j = " + j + " : digit is " + digits[j]);
}
DisplayResult();
}
else
{
amtStr = "zero";
}
} //DoConvert
private String GetTextDigit( long digit)
{
String returnStr=null;
switch ((int)digit)
{
case 1: { returnStr= " one "; break; }
case 2: { returnStr= " two "; break; }
case 3: { returnStr= " three "; break; }
case 4: { returnStr= " four "; break; }
case 5: { returnStr= " five "; break; }
case 6: { returnStr= " six "; break; }
case 7: { returnStr= " seven "; break; }
case 8: { returnStr= " eight "; break; }
case 9: { returnStr= " nine "; break; }
}
return(returnStr);
}
private String Get3DigitStr( long X)
{
String outBuff = " ";
long hundredsDigit = X / 100;
X %= 100;
long tensDigit = X / 10;
long onesDigit = X %= 10;
if (hundredsDigit>0 )
{
outBuff = GetTextDigit(hundredsDigit) + " hundred ";
}
if (tensDigit>1)
{
switch ((int)tensDigit)
{
case 2: { outBuff += " twenty "; break; }
case 3: { outBuff += " thirty "; break; }
case 4: { outBuff += " fourty "; break; }
case 5: { outBuff += " fifty "; break; }
case 6: { outBuff += " sixty "; break; }
case 7: { outBuff += " seventy "; break; }
case 8: { outBuff += " eighty "; break; }
case 9: { outBuff += " ninety "; break; }
}
}
else if (tensDigit==1)
{
switch ((int)onesDigit)
{
case 0: { outBuff += " ten "; break; }
case 1: { outBuff += " eleven "; break; }
case 2: { outBuff += " twelve "; break; }
case 3: { outBuff += " thirteen "; break; }
case 4: { outBuff += " fourteen "; break; }
case 5: { outBuff += " fifteen "; break; }
case 6: { outBuff += " sixteen "; break; }
case 7: { outBuff += " seventeen "; break; }
case 8: { outBuff += " eighteen "; break; }
case 9: { outBuff += " twenty "; break; }
}
}
if ((onesDigit>0) && (tensDigit != 1))
{
outBuff += GetTextDigit(onesDigit);
}
return(outBuff);
} //Get3DigitStr
private void DisplayResult()
{
amtStr = " ";
if (digits[0]>0)
{
amtStr = amtStr + GetTextDigit(digits[0]) + " billion ";
}
long digits3 = digits[1]*100+digits[2]*10+digits[3];
if (digits3>0)
{
amtStr = amtStr + Get3DigitStr(digits3) + " million ";
}
digits3 = digits[4]*100 + digits[5]*10 + digits[6];
if (digits3>0)
{
amtStr = amtStr + Get3DigitStr(digits3) + " thousand ";
}
digits3 = digits[7]*100+digits[8]*10+digits[9];
if (digits3>0)
{
amtStr = amtStr + Get3DigitStr(digits3);
}
System.out.println(amtStr);
} //DisplayResult
public static void main(String args[])
{
String inBuff;
long amt;
if (args.length>0)
{
inBuff = args[0];
}
else
{
inBuff=null;
while (inBuff== null)
{
inBuff = JOptionPane.showInputDialog(" Please input the number");
}
}
amt = Long.parseLong(inBuff);
AmountToText amt2Txt = new AmountToText(amt);
amt2Txt.DoConvert();
System.out.println(amt2Txt.GetAmtStr());
}
}

Andy C.
09/04/17