Input :14000 Output : hundred and fourteen thousand.(wrong)
Desired output : fourteen hundred thousand.
Answers were Sorted based on User's Feedback
public class Converter
{
private double getPlace( String number ){
switch( number.length() ){
case 1:
return DefinePlace.UNITS;
case 2:
return DefinePlace.TENS;
case 3:
return DefinePlace.HUNDREDS;
case 4:
return DefinePlace.THOUSANDS;
case 5:
return DefinePlace.TENTHOUSANDS;
case 6:
return DefinePlace.LAKHS;
case 7:
return DefinePlace.TENLAKHS;
case 8:
return DefinePlace.CRORES;
case 9:
return DefinePlace.TENCRORES;
}//switch
return 0.0;
}// getPlace
private String getWord( int number ){
switch( number ){
case 1:
return "One";
case 2:
return "Two";
case 3:
return "Three";
case 4:
return "Four";
case 5:
return "Five";
case 6:
return "Six";
case 7:
return "Seven";
case 8:
return "Eight";
case 9:
return "Nine";
case 0:
return "Zero";
case 10:
return "Ten";
case 11:
return "Eleven";
case 12:
return "Tweleve";
case 13:
return "Thirteen";
case 14:
return "Forteen";
case 15:
return "Fifteen";
case 16:
return "Sixteen";
case 17:
return "Seventeen";
case 18:
return "Eighteen";
case 19:
return "Ninteen";
case 20:
return "Twenty";
case 30:
return "Thirty";
case 40:
return "Forty";
case 50:
return "Fifty";
case 60:
return "Sixty";
case 70:
return "Seventy";
case 80:
return "Eighty";
case 90:
return "Ninty";
case 100:
return "Hundred";
} //switch
return "";
} //getWord
private String cleanNumber( String number ){
String cleanedNumber = "";
cleanedNumber = number.replace( '.', ' ' ).replaceAll( " ",
"" );
cleanedNumber = cleanedNumber.replace( ',', ' '
).replaceAll( " ", "" );
if( cleanedNumber.startsWith( "0" ) )
cleanedNumber = cleanedNumber.replaceFirst( "0", "" );
return cleanedNumber;
} //cleanNumber
public String convertNumber( String number ){
number = cleanNumber( number );
double num = 0.0;
try{
num = Double.parseDouble( number );
}catch( Exception e ){
return "Invalid Number Sent to Convert";
} //catch
String returnValue = "";
while( num > 0 ){
number = "" + (int)num;
double place = getPlace(number);
if( place == DefinePlace.TENS || place ==
DefinePlace.TENTHOUSANDS || place == DefinePlace.TENLAKHS ||
place == DefinePlace.TENCRORES ){
int subNum = Integer.parseInt( number.charAt(0) + "" +
number.charAt(1) );
if( subNum >= 21 && (subNum%10) != 0 ){
returnValue += getWord( Integer.parseInt( "" +
number.charAt(0) ) * 10 ) + " " + getWord( subNum%10 ) ;
} //if
else{
returnValue += getWord(subNum);
}//else
if( place == DefinePlace.TENS ){
num = 0;
}//if
else if( place == DefinePlace.TENTHOUSANDS ){
num -= subNum * DefinePlace.THOUSANDS;
returnValue += " Thousands ";
}//if
else if( place == DefinePlace.TENLAKHS ){
num -= subNum * DefinePlace.LAKHS;
returnValue += " Lakhs ";
}//if
else if( place == DefinePlace.TENCRORES ){
num -= subNum * DefinePlace.CRORES;
returnValue += " Crores ";
}//if
}//if
else{
int subNum = Integer.parseInt( "" + number.charAt(0) );
returnValue += getWord( subNum );
if( place == DefinePlace.UNITS ){
num = 0;
}//if
else if( place == DefinePlace.HUNDREDS ){
num -= subNum * DefinePlace.HUNDREDS;
returnValue += " Hundred ";
}//if
else if( place == DefinePlace.THOUSANDS ){
num -= subNum * DefinePlace.THOUSANDS;
returnValue += " Thousand ";
}//if
else if( place == DefinePlace.LAKHS ){
num -= subNum * DefinePlace.LAKHS;
returnValue += " Lakh ";
}//if
else if( place == DefinePlace.CRORES ){
num -= subNum * DefinePlace.CRORES;
returnValue += " Crore ";
}//if
}//else
}//while
return returnValue;
}//convert number
public static void main( String args[] ){
Converter cv = new Converter();
if( args.length >= 1 )
{
for( int i=0; i<args.length; i++ )
System.out.println( "Given Number : " + args[i] +
"\nConverted: " + cv.convertNumber(args[i]) + "\n\n" );
System.exit(0);
}
System.out.println( "Given Number : 14000\nConverted: " +
cv.convertNumber("14000") + "\n\n" );
}//main
} //class
class DefinePlace{
public static final double UNITS = 1;
public static final double TENS = 10 * UNITS;
public static final double HUNDREDS = 10 * TENS;
public static final double THOUSANDS = 10 * HUNDREDS;
public static final double TENTHOUSANDS = 10 * THOUSANDS;
public static final double LAKHS = 10 * TENTHOUSANDS;
public static final double TENLAKHS = 10 * LAKHS;
public static final double CRORES = 10 * TENLAKHS;
public static final double TENCRORES = 10 * CRORES;
} //class
| Is This Answer Correct ? | 4 Yes | 0 No |
Answer / anubhav gupta
public class ToText {
int num,length;
boolean length_odd;
//determine the length of a given number
int getlength(int x)
{
int temp=x,len=0;
while(temp!=0)
{
temp=temp/10;
len++;
}
return len;
}
//constructor
ToText(int no)
{
num=no;
length=getlength(num);
if(num==0)
{System.out.println("zero"+ length);
length--;
}
theString(num);
}
//naming every number from 0 to 20 and we can construct
//the rest of them easily
void getname(int x)
{
switch(x)
{
case 0:System.out.print( " ");
break;
case 1: System.out.print( " one");break;
case 2: System.out.print( " two");break;
case 3 : System.out.print( " three");break;
case 4 : System.out.print( " four");break;
case 5 : System.out.print( " five");break;
case 6: System.out.print( " six");break;
case 7: System.out.print( " seven");break;
case 8: System.out.print( " eight");break;
case 9 : System.out.print( " nine");break;
case 10 : System.out.print( " ten");break;
case 11 : System.out.print( " eleven");break;
case 12 : System.out.print( " twelve");break;
case 13: System.out.print( " thirteen");break;
case 14: System.out.print( " fourteen");break;
case 15 : System.out.print( " fifteen");break;
case 16: System.out.print( " sixteen");break;
case 17: System.out.print( " seventeen");break;
case 18: System.out.print( " eighteen");break;
case 19: System.out.print( " nineteen");break;
case 20: System.out.print( " twenty");break;
case 30: System.out.print( " thirty");break;
case 40: System.out.print( " fourty");break;
case 50: System.out.print( " fifty");break;
case 60: System.out.print( " sixty");break;
case 70: System.out.print( " seventy");break;
case 80: System.out.print( " eighty");break;
case 90: System.out.print( " ninety");break;
default:break;
}
}
//no of digit from right side,(counting starts from 1
and not 0)
void gettxt(int x)
{
switch(x)
{
case 3:System.out.print(" hundred");break;
case 4:System.out.print(" thousand");break;
case 6:System.out.print(" lakh");break;
case 8:System.out.print(" crore");break;
case 10:System.out.print(" arab");break;
default:break;
}
}
//Main Logic--start from left if length is odd them
//fetch two digits
//from left and convert them else if length is even
//fetch one digit
//from left and convert it.eg: 12345, it's length is 5
//which is odd
//it will fetch 12 and convert it to text.eg: 2,345
//it's length is even
//therefore it will fetch 2 and convert it.
//for the last three digit ...
//first we will fetch the third last digit only and
//convert it.
//then we will fetch the last to digit together and
//convert them.
//also consider the case when user enters 0.
void theString(int x)
{
if(length<=0)
System.exit(0);
int temp,temp1;
if(length%2==1)
length_odd=true;
else length_odd=false;
//last three digit logic
if((length==3)||(length==1))
length_odd=false;
else if(length==2)
length_odd=true;
//---------
if(length_odd)
{
temp=x/(int)(Math.pow(10,length-2));
if(temp>20)
{
temp1=temp%10;
temp=temp/10;
temp =temp*10;
if((temp1==0)&&(temp==0)){length-=2;}
else{
getname(temp);
getname(temp1);
length--;
gettxt(length);
length--;
}
}
else
{
if(temp==0){length-=2;}
else{
getname(temp);
length--;
gettxt(length);
length--;
}
}
num=num%(int)(Math.pow(10,length));
}
else if(length_odd==false)
{
temp=x/(int)(Math.pow(10, length-1));
if(temp==0)
{}
else{
getname(temp);
gettxt(length);
}
length--;
num=num%(int)(Math.pow(10,length));
}
theString(num);
}
public static void main(String[] args)
{
ToText t=new ToText(1234);
}
}
| Is This Answer Correct ? | 2 Yes | 0 No |
What is meant by framework in java?
What is the difference between ec2 and lambda?
What is interceptors in java?
How do you run an executable jar file?
What is native api in java?
Topic- looping,function overloading,nesting ,polymorphism. Aim - to write a function with a name buzz-buds,that will check whether the given numbers are buddies or not on the basis of no. of parameters passed during function calling.
Explain different way of using thread?
Why do we need framework in java?
What is an action class in java?
Is java built on c?
I am new to jsf rich faces. I am using rich faces datatable rich:datatable. On entering the value, values get filtered in table. Now how can i get the value i selected in backing bean?
1.can i use super keyword in normal class(not inheritance) to call any method?if so how can i call particular variable? 2.In the inheritance how can i access the particular variable from the base class(it containing 5 variables) using super keyword?