Input :14000 Output : hundred and fourteen thousand.(wrong)
Desired output : fourteen hundred thousand.

Answers were Sorted based on User's Feedback



Input :14000 Output : hundred and fourteen thousand.(wrong) Desired output : fourteen hundred thou..

Answer / dara

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

Input :14000 Output : hundred and fourteen thousand.(wrong) Desired output : fourteen hundred thou..

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

Post New Answer

More Java Related AllOther Interview Questions

What does persist mean in java?

0 Answers  


why is java not 100% oops?

5 Answers   IBM,


why java has the size of character 2 byte ,while in c/c++ it is of 1 byte?

8 Answers   IBS,


What is tier in java?

0 Answers  


Why do we only use the main method to start a program?

0 Answers  






What is java ioc?

0 Answers  


What is the use of flatmap in java 8?

0 Answers  


ioc vs dependency injection?

0 Answers  


Can optional be null?

0 Answers  


Are streams faster than for loops?

0 Answers  


What does public static void main(string[]) mean?

0 Answers  


What is the difference between jar and executable jar?

0 Answers  


Categories