July 02, 2016

Number to word convertor in java

How to convert a numeric value into English words in java



Source code


import java.util.Scanner;

/**
 * Number to word convertor
 */

/**
 * @author consfedkozhikode@gmail.com
 *
 */
public class NumberToWord {
   
    private String[] ones = {"","one","two","three","four","five","six","seven","eight","nine","ten"};
    private String[] twenties = {"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"};
    private String[] tens = {"","ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
    private String[] hundreds = {"ones","tens","hundred","thousand","thousand","lakhs","lakhs","crore","crore"};
    private String word = "";
    private String tempWord = null;
    private int lastDigit = 0;
    /**
     * @param args
     */
    public static void main(String[] args) {
        NumberToWord numberToWord = new NumberToWord();
        int number = 0;
        Scanner input = new Scanner(System.in);
        System.out.println(" Enter the number : ");
        number = input.nextInt();
        int output = numberToWord.convertToWord(number,0);
    }

    private int convertToWord(int number,int count) {
                int quotient = number/10;
                int reminder = number % 10;
                getTheWord(reminder,count);
                if(quotient<=0){
                    word = tempWord + word;
                    System.out.println(" output : "+word);
                    return 0;
                }else
                {
                    return convertToWord(quotient,count+1);
                }
    }

    private void getTheWord(int reminder,int placeValue) {
        String newWord = null;
        switch (placeValue) {
       
        case 0:
            newWord = " "+ones[reminder];
            break;
        case 1:
            if(reminder==1){
                word = word; //skip
                int number = Integer.parseInt(String.valueOf(reminder+""+lastDigit));
                newWord = " "+twenties[number];
            }else{
                word = tempWord + word;
                newWord = " "+tens[reminder];
            }
            break;
        case 2:
            word = tempWord + word; // adding tens
            newWord = " "+ones[reminder]+" "+hundreds[placeValue]+" and";
            break;
        case 3:
            word = tempWord + word; //adding hundred
            newWord = " "+ones[reminder]+" "+hundreds[placeValue];
            break;
        case 4:
            if(reminder==1){
                word = word;
                int number = Integer.parseInt(String.valueOf(reminder+""+lastDigit));
                newWord = ""+twenties[number]+" "+hundreds[placeValue]+"";
            }else{
                word = tempWord + word;
                newWord = " "+tens[reminder]+"";
            }
            break;

        default:
            System.out.println(" This is default statement, it won't print ");
            break;
        }
        lastDigit = reminder;
        tempWord = newWord;
    }

}


Output

img for number to word covertor output
Number to english word in java

Description


Number to word converter java program converts numeric value into words,  currently the program will convert up to thousands. The same logic is applied for lakhs and crores so you can extend the project, only thing you have to do is to increase case statement in switch (repeating case 3 and case 4).
       In this program I pass the user inputted number to recursive function convertToWord(), A recursive function is a function that call itself. convertToWord() function will find the quotient and reminder of the number in each calls, the reminder is send getTheWord() function where a switch case will convert the number into corresponding words.

Most useful code

Java reference ( Buy a copy )

Similar posts


Finding HCF and LCM

Finding Factorial using recursion

Fibonacci series

Binary search in java

Palindrome in java

Geometric mean using java 

Prime number in java

Print triangle in java

Find missing number in java




author : +belazy


Ever tried to develop a tic toc toe game, you can try our code, multiplayer and with computer the whole source code will be available soon, stay update




http://javabelazy.blogspot.in/

13 comments:

  1. Excellent beat ! I wish to apprentice while you amend your web site,
    how could i subscribe for a blog site? The account helped me a
    acceptable deal. I had been a little bit acquainted of this your
    broadcast offered bright clear idea

    ReplyDelete
  2. Magnificent beat ! I wish to apprentice while you amend your site, how could i subscribe for a blog site?
    The account helped me a acceptable deal. I had been tiny bit
    acquainted of this your broadcast provided bright clear idea

    ReplyDelete
  3. Hi, i think that i saw you visited my weblog thus i came
    to ?return the favor?.I am trying to find things to improve my website!I suppose its ok to
    use a few of your ideas!!

    ReplyDelete
  4. Hello I am so glad I found your web site, I really found you by error, while I was searching on Askjeeve
    for something else, Regardless I am here
    now and would just like to say kudos for a remarkable post and
    a all round interesting blog (I also love the theme/design), I don?t have time to browse
    it all at the minute but I have saved it and also included your RSS feeds, so
    when I have time I will be back to read more, Please do keep
    up the awesome jo.

    ReplyDelete
  5. I?m impressed, I must say. Rarely do I encounter a
    blog that?s equally educative and entertaining, and let me tell you, you have hit the nail on the head.

    The issue is something too few men and women are speaking intelligently about.
    Now i'm very happy that I found this in my search for something regarding this.

    ReplyDelete
  6. This is very fascinating, You are an overly skilled blogger.
    I've joined your rss feed and stay up for in search of extra of your excellent post.
    Additionally, I have shared your website in my
    social networks

    ReplyDelete
  7. You really make it seem so easy with your presentation but I find this matter to be actually something
    that I think I would never understand. It seems too complicated and very broad for me.
    I'm looking forward for your next post, I'll try to
    get the hang of it!

    ReplyDelete
  8. I simply desired to thank you so much once again. I am not sure the things that
    I would have taken care of in the absence of the type of techniques shared by you on such theme.
    Previously it was a traumatic circumstance in my opinion, however ,
    taking a look at a professional form you treated the issue made me to weep with gladness.
    Now i'm happier for your work and believe
    you recognize what a powerful job your are providing educating many people all through
    a blog. Probably you have never met any of us.

    ReplyDelete
  9. fantastic points altogether, you simply received a logo new reader.
    What may you suggest in regards to your submit that you simply made
    some days ago? Any certain?

    ReplyDelete
  10. For most up-to-date information you have to go to see world-wide-web and on internet I found this site as a best website for
    newest updates.

    ReplyDelete
  11. Pretty nice post. I just stumbled upon your blog and wished to say that I have really
    enjoyed browsing your blog posts. After all I
    will be subscribing to your rss feed and I hope you write again very
    soon!

    ReplyDelete
  12. Some genuinely prime articles on this internet site, bookmarked.

    ReplyDelete
  13. I'm amazed, I have to admit. Rarely do I come across a blog that's equally educative and engaging,
    and without a doubt, you've hit the nail on the head.
    The issue is an issue that too few men and women are speaking intelligently about.
    I'm very happy that I came across this during my hunt for something concerning this.

    ReplyDelete

Your feedback may help others !!!

Facebook comments