Java Basic Problems

Java: Integer (numeric) to String (word) conversion

Here is a little example of java where I have converted the integer or numeric value,  entered by the user, to String or word.

The given code will work for 1 to 1999. You can increase the limit by doing little change  in the given code.

 Here is the full code example:

import java.io.*;

class numerator

{

static String input;

static int intInput;

static String strRemainder;

static int intRemainder;

static String inWord=””;

static int firstCount=0;

                        public void myNumerator()

                        {

                                                            try

                                                            {

                        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

                                                            System.out.println(“\n”);

                                                            System.out.println(“Enter the Numeric:”);

                                                            System.out.println(“\n”);

                                                            input=br.readLine();

                                                            intInput=Integer.parseInt(input);

                                                            System.out.println(“\n”);

                                                            System.out.println(“You  have Entered:”+intInput);

                                                            beforeCalculator(intInput);

                                                            System.out.println(“\n”);

                                                            System.out.println(“In Word:”+inWord);

                                                            }

                                                            catch(Exception e)

                                                            {

                                                            System.out.println(“Error:”+e.getMessage());

                                                            }

                        }

                                                                        public void beforeCalculator(int inputa)

                                                                        {

                                                                        int length;

                                                                        String dividor=”1″;

                                                                        int intDividor;

                                                                        int intFirstValue;

                                                                        String strFirstValue=””;

                                                                        if (calculator(inputa).trim().toString()==”0″)

                                                                                    {

                                                                                    firstCount++;

                                                                                    strRemainder=Integer.toString(inputa);   

                                                                                    length=strRemainder.length();           

                                                                                    for(int i=0;i<length-1;i++)

                                                                                    {

                                                                                    dividor=dividor+”0″;

                                                                                    }

                                                                          intDividor=Integer.parseInt(dividor);          

                                                                          intFirstValue= inputa/intDividor;   

                                                                          intRemainder=inputa%intDividor;   

                                                                          int intRemainval= intFirstValue* intDividor;

                                                                          strFirstValue=calculator(intRemainval);  

                                                                          inWord=inWord+” “+strFirstValue;

                                                                          beforeCalculator(intRemainder);

                                                                        }

                                                                        else

                                                                        {

                                                                        //beforeCalculator(intRemainval);

                                                                          if(firstCount==0)

                                                                          {

                                                                          inWord=calculator(inputa);

                                                                          }

                                                                          else

                                                                          {

                                                                          inWord=inWord+” “+calculator(intRemainder);

                                                                          }

                                                                        }

                                                            }

                                      private String calculator(int d)

                                                                        {

                                                                        switch(d)

                                                                                                            {

                                                                                                            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 10:

                                                                                                            return “Ten”;

case 11:

return “Eleven”;

case 12:

return “Twelve”;

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 “Ninety”;

                                                                                                            case 100:

                                                                                                            return “One Hundred”;

                                                                                                            case 200:

                                                                                                            return “Two Hundred”;

                                                                                                            case 300:

                                                                                                            return “Three Hundred”;

                                                                                                            case 400:

                                                                                                            return “Four Hundred”;

                                                                                                            case 500:

                                                                                                            return “Five Hundred”;

                                                                                                            case 600:

                                                                                                            return “Six Hundred”;

                                                                                                            case 700:

                                                                                                            return “Seven Hundred”;

                                                                                                            case 800:

                                                                                                            return “Eight Hundred”;

                                                                                                            case 900:

                                                                                                            return “Nine Hundred”;

                                                                                                            case 1000:

                                                                                                            return “One Thousand”;

                                                                                                            default:

                                                                                                            return “0”;

                                                                                                            }

                                                                        }

 public static void main(String args[])

{

numerator x=new numerator();

x.myNumerator();

}

}

OUTPUT:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s