Results 1 to 6 of 6

Thread: How to convert the digit into word in Java?

  1. #1
    Join Date
    Dec 2009
    Posts
    23

    How to convert the digit into word in Java?

    I am beginner programmer in the programming world. I am learning the programming language like Core java, DBMS, Html, C sharp, C++,etc. I just loves doing programming. These days i am working(programming) in Java. I do programming on DELL Studio One 19 desktop computer. Actually i want create a program in Java which can convert the digit entered into words. Example:
    Enter a number:786
    Output:seven hundred eighty-six

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to convert the digit into word in Java?

    I am also learning the Core Java language right now. I have also tried many ways to achieve the goal you wanted (i.e to convert the digit into words.). I will tell the method how to achieve it, but you have to do complete hard coding.
    String[] Arr={"zero", "seven", eight", "six"};
    int nos = 0;
    System.out.println(Arr[nos]);
    nos = 3;
    System.out.println(Arr[nos]);
    Hope your problem get solve.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to convert the digit into word in Java?

    class constNostoWord
    {
    String[] unitpl ={"", " One", " Two", " Three", " Four", " Five",
    " Six", " Seven", " Eight", " Nine", " Ten", " Eleven", " Twelve",
    " Thirteen", " Fourteen", " Fifteen", " Sixteen", " Seventeen",
    " Eighteen", " Nineteen"};
    String[] tenspl = {"", "Ten", " Twenty", " Thirty", " Forty", " Fifty",
    " Sixty", " Seventy", " Eighty"," Ninety"};
    String[] digitval = {"", " Hundred", " Thousand", " Lakh", " Crore"};
    int r1;


    //Counting the number of digits in the input number
    int numCount(int nos)
    {
    int count=0;

    while (nos>0)
    {
    r1 = nos%10;
    count++;
    nos = nos / 10;
    }

    return count;
    }


    //Function for Conversion of two digit

    String twonos(int nosq)
    {
    int numsr, numsq;
    String wrd=\"";

    numsq = nosq / 10;
    numsr = nosq % 10;

    if (numsq>19)
    {
    wrd=wrd+tenspl[numsq]+unitpl[numsr];
    }
    else
    {
    wrd = wrd+unitpl[numsq];
    }

    return wrd;
    }

    //Function for Conversion of three digit

    String threenos(int numsq)
    {
    int numsr, nosq;
    String wrd = "";

    nosq = numsq / 100;
    numsr = numsq % 100;

    if (numsr == 0)
    {
    wrd = wrd + unitpl[nosq]+digitval[1];
    }
    else
    {
    wrd = wrd +unitpl[nosq]+digitval[1]+" and"+twonos(numsr);
    }
    return wrd;

    }

    }

    class originalNosToWrd

    {

    public static void main(String[] args) throws Exception
    {

    //Defining variables quo is quotient, rem is remainder

    int lens, quo=0, rem=0;
    String wrd = " ";
    String Str1 = "Rupees";
    constNostoWord n = new constNostoWord();
    int nos = Integer.parseInt(args[0]);

    if (nos <= 0) System.out.println(\"Zero or Negative number not for conversion");

    while (nos>0)
    {

    lens = n.numberCount(nos);

    //Take the length of the number and do letter conversion

    switch (lens)

    {
    case 8:
    quo=nos/10000000;
    rem=nos%10000000;
    wrd = n.twonos(quo);
    Str1 = Str1+wrd+n.digitval[4];
    nos = rem;
    break;

    case 7:
    case 6:
    quo=nos/100000;
    rem=nos%100000;
    wrd = n.twonos(quo);
    Str1 = Str1+wrd+n.digitval[3];
    nos = rem;
    break;

    case 5:
    case 4:

    quo=nos/1000;
    ren=nos%1000;
    wrd = n.twonos(quo);
    Str1 = Str1+wrd+n.digitval[2];
    nos = rem;
    break;

    case 3:


    if (lens == 3)
    rem = nos;
    wrd = n.threenos(rem);
    Str1 = Str1 + wrd;
    nos = 0;
    break;

    case 2:

    wrd= n.twonos(nos);
    Str1 = Str1 + wrd;
    nos=0;
    break;

    case 1:
    Str1 = Str1 + n.unitpl[nos];
    nos=0;
    break;
    default:

    nos=0;
    System.out.println(\"Exceeding Crore. No conversion");
    System.exit(1);


    }
    if (nos==0)
    System.out.println(Str1+\" Only");
    }

    }
    Hope this program will solve your problem.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to convert the digit into word in Java?

    If would be in your place then i would choose the methods of making an ArrayList of strings and not the list (which is more versatile, but list of Strings will do as well) and then after that make a string variable that you can do as
    x = x + number.toString();
    Perhaps some if statements will solve the other have of the problem
    if (number ==1) then list.get(n-1).
    Hope you will be able to get the solution to your problem.

  5. #5
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to convert the digit into word in Java?

    According to me you will have to use the two characters arrays in you program. Example: The 1st array will be able to classify as units place. It will contain zero, one, two, three, four, till nineteen. And 2nd array will be able classify as tens place. It will contain twenty, thirty, till ninety. And for starting, you need to divide the input number's by 100. If the result o of the number is greater than zero, then use this result number as an index into the first units place array to print the text at that index. And so on. Hope you will get the exact solution.

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to convert the digit into word in Java?

    I also have the somewhat same problem of converting the digits(i.e specially decimal) into word in Java. The homework project that my professor gave me says that i have to convert decimal into words with the range -1 TO -100000 and +1 TO +100000. Please anyone over here can help me, i tried many ways to achieve this, but didn't got any success. Please help me out.

Similar Threads

  1. How to convert pdf to word or PPT?
    By youtuy in forum Windows Software
    Replies: 4
    Last Post: 28-01-2013, 09:00 AM
  2. Java API to convert Microsoft Word to PDF
    By Arlo in forum Software Development
    Replies: 11
    Last Post: 02-05-2011, 04:26 PM
  3. 5 digit integer which has 10 digit square
    By sayanmaji in forum Education Career and Job Discussions
    Replies: 2
    Last Post: 04-06-2010, 12:19 AM
  4. How to Convert Word doc to jpg?
    By Silent~Kid in forum Windows Software
    Replies: 5
    Last Post: 08-03-2010, 02:09 PM
  5. Replies: 3
    Last Post: 17-11-2009, 09:22 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,416,733.24575 seconds with 17 queries