I need help in this question:
Java application
Read in numbers between 0-999 until the user enters -1. Print out the number in words. Use Australian English grammar. Note the spelling of “four”, “fourteen” and “forty”. Sample I/O: Number: 17 seventeen Number: 103 one hundred and three Number: 800 eight hundred Number: 42 forty two Number: -1 Note: Strict grammar tells us to hyphenate compound numbers. PLATE expects you to just separate each word by a space. For simplicity, you should just separate each word by a space. Thank you,
Expert Answer
/**
*
* @author Akshay Bisht
*/
import java.util.*;
public class num2Words
{
private static String inp;
private static int nob;
private static String[] arr=
{“”,
” One”,
” Two”,
” Three”,
” Four”,
” Five”,
” Six”,
” Seven”,
” Eight”,
” Nine”
};
private static String[] nun10=
{” Ten”,
” Eleven”,
” Twelve”,
” Thirteen”,
” Fourteen”,
” Fifteen”,
” Sixteen”,
” Seventeen”,
” Eighteen”,
” Nineteen”
};
private static String[] nuum10s=
{ ” Twenty”,
” Thirty”,
” Forty”,
” Fifty”,
” Sixty”,
” Seventy”,
” Eighty”,
” Ninety”
};
private static String[] maxDigi=
{“”,
“”,
” Hundred”,
” Thousand”,
” Lakh”,
” Crore”
};
public String cnvNum2Words(int nob)
{
inp=numToWordsss(nob);
String cnvrtd=””;
int posit=1;
boolean hundreds=false;
while(inp.length()> 0)
{
if(posit==1)
{ if(inp.length()>= 2)
{
String tempo=inp.substring(inp.length()-2,inp.length());
inp=inp.substring(0,inp.length()-2);
cnvrtd+=digiWords(tempo);
}
else if(inp.length()==1)
{
cnvrtd+=digiWords(inp);
inp=””;
}
posit++;
}
else if(posit==2)
{
String tempo=inp.substring(inp.length()-1,inp.length());
inp=inp.substring(0,inp.length()-1);
if(cnvrtd.length()> 0&&digiWords(tempo)!=””)
{
cnvrtd=(digiWords(tempo)+maxDigi[posit]+” and”)+cnvrtd;
hundreds=true;
}
else
{
if
(digiWords(tempo)==””);
else
cnvrtd=(digiWords(tempo)+maxDigi[posit])+cnvrtd;hundreds=true;
}
posit++;
}
else if(posit > 2)
{
if(inp.length()>= 2)
{
String tempo=inp.substring(inp.length()-2,inp.length());
inp=inp.substring(0,inp.length()-2);
if(!hundreds&&cnvrtd.length()> 0)
cnvrtd=digiWords(tempo)+maxDigi[posit]+” and”+cnvrtd;
else
{
if(digiWords(tempo)==””) ;
else
cnvrtd=digiWords(tempo)+maxDigi[posit]+cnvrtd;
}
}
else if(inp.length()==1)
{
if(!hundreds&&cnvrtd.length()> 0)
cnvrtd=digiWords(inp)+maxDigi[posit]+” and”+cnvrtd;
else
{
if(digiWords(inp)==””) ;
else
cnvrtd=digiWords(inp)+maxDigi[posit]+cnvrtd;
inp=””;
}
}
posit++;
}
}
return cnvrtd;
}
private String digiWords(String tempo)
{
String cnvrtd=””;
for(int uu=tempo.length()-1;uu >= 0;uu–)
{ int ch=tempo.charAt(uu)-48;
if(uu==0&&ch>1 && tempo.length()> 1)
cnvrtd=nuum10s[ch-2]+cnvrtd;
else if(uu==0&&ch==1&&tempo.length()==2)
{
int add=0;
for(int qq=0;qq < 2;qq++)
add=(add*10)+(tempo.charAt(qq)-48);
return nun10[add-10];
}
else
{
if(ch > 0)
cnvrtd=arr[ch]+cnvrtd;
}
}
return cnvrtd;
}
private String numToWordsss(int io)
{
String nob=””;
while(io!=0)
{
nob=((char)((io%10)+48))+nob;
io/=10;
}
return nob;
}
private void inpNob()
{
Scanner in=new Scanner(System.in);
try
{
System.out.print(“Please enter number to Convert into Words : “);
nob=in.nextInt();
}
catch(Exception e)
{
System.out.println(“Number should be Less than 1 Arab “);
System.exit(1);
}
}
public static void main(String[] args)
{
num2Words numm=new num2Words();
numm.inpNob();
System.out.println(“inp in Words : “+numm.cnvNum2Words(nob));
}
}
OUTPUT: