Convert Number to Words
Kindly Help me Explain this Line of Codes.
Line by Line if possible.
It was given to me by my friend.

Hoping for Some Help.
Thank you very much.

import java.util.Scanner;
public class WordNumber{
    public static void main(String[]args){
        for(int i = 0; i < 20; i++){
            Scanner input = new Scanner(System.in);
            System.out.print("Enter a Non Floating Point Number: ");
            long x = input.nextLong();
            System.out.println(makeWord(x)); //Please Explain
        }//end for
    }//end main

//Please Explain
    private static final String[] numbers = {
        "zero","one","two","three","four","five","six",
        "seven","eight","nine","ten","eleven","twelve",
        "thirteen","fourteen","fifteen","sixteen","seventeen",
        "eighteen","nineteen"};
    private static final String[] tens = {
        "","teen","twenty","thirty","fourty","fifty",
        "sixty","seventy","eighty","ninety"};

//Please Explain
    public static String makeWord(long number){
        String s = "";

        if(number<0){
            s="negative ";
            number*=-1;
        }

        s+=toWord(number);
        s = s.replace("-zero","");

        return s;
    }//end of makeWord

//Please Explain
    private static String toWord(long number){

       if(number>=1000000000){
            return toWord(number/1000000000)+"-billion-"+toWord(number%1000000000);
        }
        else if(number>=1000000){
            return toWord(number/1000000)+"-million-"+toWord(number%1000000);
        }
        else if(number>=1000){
            return "" +toWord(number/1000)+"-thousand-"+toWord(number%1000);
        }
        else if(number>=100){
            return numbers[(int)number/100] +"-hundred-"+toWord((long)number%(long)100);//Please Explain
        }
       else if(number>=20){
            return tens[(int)number/10] + "-" + toWord(number%10);
        }
        else if(number>18){
            return numbers[(int)number%10]+"teen";
        }
        else{
            return numbers[(int)number];
        }

    }//end of toWord

}//end class

Recommended Answers

All 4 Replies

Well, well, afraid you're going to be asked to explain your code to your instructor? Or do you have to turn in a written explanation/description of it with the code?

In any case, if you had done your assignment, rather than copying it, you would probably understand it.

P.S. Whether any of the above is true, or not, don't bother protesting, as nobody here is going to believe it. We see far to many "do my assignment for me" requests to believe that you "got this from a friend" for anything other than a homework assignment.

Well, well, afraid you're going to be asked to explain your code to your instructor? Or do you have to turn in a written explanation/description of it with the code?

In any case, if you had done your assignment, rather than copying it, you would probably understand it.

P.S. Whether any of the above is true, or not, don't bother protesting, as nobody here is going to believe it. We see far to many "do my assignment for me" requests to believe that you "got this from a friend" for anything other than a homework assignment.

well, i'm new in java.
he explained some like this.
its a final variable which is String[] numbers and String[] tens.
static it is executed according to its appearance.
correct?

private static final String[] numbers = {
        "zero","one","two","three","four","five","six",
        "seven","eight","nine","ten","eleven","twelve",
        "thirteen","fourteen","fifteen","sixteen","seventeen",
        "eighteen","nineteen"};
    private static final String[] tens = {
        "","teen","twenty","thirty","fourty","fifty",
        "sixty","seventy","eighty","ninety"};

here's a static initializers which being executed in order.
right?

public static String makeWord(long number){
        String s = "";

        if(number<0){
            s="negative ";
            number*=-1;
        }

        s+=toWord(number);
        s = s.replace("-zero","");

        return s;
    }//end of makeWord

    private static String toWord(long number){

       if(number>=1000000000){
            return toWord(number/1000000000)+"-billion-"+toWord(number%1000000000);
        }
        else if(number>=1000000){
            return toWord(number/1000000)+"-million-"+toWord(number%1000000);
        }
        else if(number>=1000){
            return "" +toWord(number/1000)+"-thousand-"+toWord(number%1000);
        }
        else if(number>=100){
            return numbers[(int)number/100] +"-hundred-"+toWord((long)number%(long)100);
        }
       else if(number>=20){
            return tens[(int)number/10] + "-" + toWord(number%10);
        }
        else if(number>18){
            return numbers[(int)number%10]+"teen";
        }
        else{
            return numbers[(int)number];
        }

    }//end of toWord

kinda confused. :sad:

adamson university?

Try to give a proper description to the thread.

Don't put "Please Help" as the thread.

Obviously everybody here are going to help you, if you show your effort.

And if you are not putting any effort no body will help you obviously.

So try to put the proper Title for the Threads you post in future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.