Hi guys i am new here
i want to help about:
how to convert digit(number) into word
for example:
Enter a number:123
Output:one hundred twenty-three

you have to "hard code it" with use of array for example

String[] myArray={"zero", "one", two", "three"};
int num = 0;
System.out.println(myArray[num]);
num = 3;
System.out.println(myArray[num]);

first return will be string zero and second one will be three

commented: It didn't solve the problem +0
commented: Useful hints +17

which won't provide a correct solution in almost any situation :)
You'll need a lot more logic than that, but it could be used as a starting point.

Instead of an array though you should really consider using an enum.

This is going to be harder than you think due to the fact that numbers are represented differently depending on the position in the number the specific digit is in. For example, if a 5 is in the one's slot, it is represented as "five", whereas in the ten's spot it is fifty-[ones digit number(unless zero)], then back to "five" for the hundreds and so on.

In a nutshell, you would need a fair amount of logic as jwenting stated, just figured I would elaborate some more.

Regards,

Tyler S. Breton

I would approach this making an ArrayList of strings instead of a list (tad more versitile, but list of Strings will do as well) and then create a string variable that you can do x = x + number.toString();

Perhaps some if statements will solve the other have of the problem ;)

if (number ==1) then list.get(n-1);

making an ArrayList of strings instead of a list (tad more versitile ...

How so? An ArrayList is a List, unless you mean to use ArrayList instead of an array of Strings.

I mis spoke... I had some python crossing my java and things were fuzzy... but you are right... Array List instead of an Array of strings..

How do I convert the Integer to String by:
1. Asking the user to input the number (Eg: 704 = Seven Hundred and Four)

2. Display the output in JTextArea.

Thanks for your support.

How do I convert the Integer to String by:
1. Asking the user to input the number (Eg: 704 = Seven Hundred and Four)

2. Display the output in JTextArea.

Thanks for your support.

Start a new thread. This is a 3 year old thread.

commented: Now it's 5 years old, but the bottom feeders are still here +17

HIYA GUYS
VERY BASIC WAY OF DOING IT AND ONLY HANDLES UP TO NUMBER 999 ALTHOUGH CAN BE ALTERED PRETTY EASILY.

THIS CODE DOES NOT USE CONFUSING ARRAYS WHICH WOULD CUT DOWN MY CODE A LOT BUT THIS CODE IS THE FULL WHACK AND THE WAY THEY WOULD TEACH SOMEONE AT UNIVERSITY AT BASIC LEVEL.

COPY AND PASTE INTO JAVA EDITOR...

OBVIOUSLY A GOOD WAY TO UNDERSTAND MY CODE PROPERLY OR ANY CODE IS TO USE THE DEBUG OPTION IN UR JAVA EDITOR TO GO THROUGH THE STEPS ONE BY ONE FOR ANY GIVEN NUMBER

THANKS BYE...

--------------------------------

// Program to convert integers into words, Ex5.5
package num2word;

import java.util.Scanner;

public class Main {

    // units Method
    public static void units(int n) {

        switch (n) {
            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;
        }
    }

    // special case Method
    public static void special(int n) {

        switch (n) {
            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;
        }
    }

    // tens Method
    public static void tens(int n) {

        switch (n) {
            case 1:
                System.out.print(" ten ");
            case 2:
                System.out.print(" twenty ");
                break;
            case 3:
                System.out.print(" thirty ");
                break;
            case 4:
                System.out.print(" forty ");
                break;
            case 5:
                System.out.print(" fifty ");
                break;
            case 6:
                System.out.print(" sixty ");
                break;
            case 7:
                System.out.print(" seventy ");
                break;
            case 8:
                System.out.print(" eighty ");
                break;
            case 9:
                System.out.print(" ninety ");
                break;
        }
    }

    // hundreds Method
    public static void hundreds(int n) {

        switch (n) {
            case 1:
                System.out.print("one hundred ");
                break;
            case 2:
                System.out.print("two hundred ");
                break;
            case 3:]
                System.out.print("three hundred ");
                break;
            case 4:
                System.out.print("four hundred ");
                break;
            case 5:
                System.out.print("five hundred ");
                break;
            case 6:
                System.out.print("six hundred ");
                break;
            case 7:
                System.out.print("seven hundred ");
                break;
            case 8:
                System.out.print("eight hundred ");
                break;
            case 9:
                System.out.print("nine hundred ");
                break;
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        // Requesting User input
        System.out.print("Enter a number between 0 and 999: ");
        int number = input.nextInt();

        // input validation
        while (number < 0 || number > 999) {
            System.out.println("Input Too Large, ");
            System.out.print("Enter a number between 0 and 999: ");
            number = input.nextInt();
        }

        // Below we start to determine what the input was by
        // categorising it using boolean

        // original number (100-999)
        if (number > 99 && number < 1000) {
            int h = number / 100;    //find the hundreds only (one, two hundred)
            hundreds(h);             //print number

            // first part of number found & printed (hundreds)
            // remamining 2 digits must be found ie tens & units

            // test if they are a special number
            int x = 0;               // initialized variable x for calculations
            x = number % 100;        // find remainder of hundreds (ie x54)
            if (x > 10 && x < 20) {  // ie is 54 a special number?
                special(x);          // print number
            }
            // if not special number -> split up into tens & units
            if (x > 0 && x < 100) {

                int tens = x / 10;   // finding the tens (forty, fifty)
                tens(tens);          // print number

                int units = x % 10;  // finding the units (one, two)
                units(units);        // print number
            }
        }

        // original number (20-99)
        if (number > 19 && number < 100) {

            int t = number / 10;     // finding the tens (forty, fifty)
            tens(t);                 // print number

            int u = number % 10;     // finding the units (one, two)
            units(u);                // print number
        }

        // original number (11-19) (Special numbers)
        if (number > 10 && number < 20) { // (Special numbers)
            special(number);         // print number
        }

        // original number (0-9)
        if (number < 10) {           // finding the units (one, two)
            units(number);           // print number
        }
    }
}
commented: ohsom man , you are agood programmer +0
commented: Far too much unnecessary YELLING -4

Inspired by the above response, I've coded a complete method that covers all values of integer numbers in Java (from Long.MIN_VALUE to Long.MAX_VALUE). Done in 100 lines ;)

For example, calling numberInWords(Long.MAX_VALUE) will print out:

nine quintillion two hundred twenty three quadrillion three hundred seventy two trillion thirty six billion eight hundred fifty four million seven hundred seventy five thousand eight hundred and seven

/** Holds the number 1-19, dual purpose for special cases (teens) **/
    private static final String[] UNITS = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
    /** Holds the tens places **/
    private static final String[] TENS = {"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninty"};
    /** Covers max value of Long **/
    private static final String[] THOUSANDS = {"", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion"};

    /**
     * Represents a number in words (seven hundred thirty four, two hundred and seven, etc...)
     *
     * The largest number this will accept is
     * <pre>999,999,999,999,999,999,999</pre> but that's okay becasue the largest
     * value of Long is <pre>9,223,372,036,854,775,807</pre>. The smallest number
     * is <pre>-9,223,372,036,854,775,807</pre> (Long.MIN_VALUE +1) due to a
     * limitation of Java.
     * @param number between Long.MIN_VALUE and Long.MAX_VALUE
     * @return the number writen in words
     */
    public static String numberInWords(long number) {
        StringBuilder sb = new StringBuilder();
        // Zero is an easy one, but not technically a number :P
        if (number == 0) {
            return "zero";
        }
        // Negative numbers are easy too
        if (number < 0) {
            sb.append("negative ");
            number = Math.abs(number);
        }

        // Log keeps track of which Thousands place we're in
        long log = 1000000000000000000L, sub = number;
        int i = 7;
        do {
            // This is the 1-999 subset of the current thousand's place
            sub = number / log;
            // Cut down number for the next loop
            number = number % log;
            // Cut down log for the next loop
            log = log / 1000;
            i--; // tracks the big number place
            if (sub != 0) {
                // If this thousandths place is not 0 (that's okay, just skip)
                // tack it on
                sb.append(hundredsInWords((int) sub));
                sb.append(" ");
                sb.append(THOUSANDS[i]);
                if (number != 0) {
                    sb.append(" ");
                }
            }
        } while (number != 0);

        return sb.toString();
    }

    /**
     * Converts a number into hundreds.
     *
     * The basic unit of the American numbering system is "hundreds". Thus
     * 1,024 = (one thousand) twenty four
     * 1,048,576 = (one million) (fourty eight thousand) (five hundred seventy six)
     * so there's no need to break it down further than that.
     * @param n between 1 and 999
     * @return
     */
    private static String hundredsInWords(int n) {
        if (n < 1 || n > 999) {
            throw new AssertionError(n); // Use assersion errors in private methods only!
        }
        StringBuilder sb = new StringBuilder();

        // Handle the "hundred", with a special provision for x01-x09
        if (n > 99) {
            sb.append(UNITS[(n / 100) - 1]);
            sb.append(" hundred");
            n = n % 100;
            if (n != 0) {
                sb.append(" ");
            }
            if(n < 10){
                sb.append("and ");
            }
        }

        // Handle the special cases and the tens place at the same time.
        if (n > 19) {
            sb.append(TENS[(n / 10) - 1]);
            n = n % 10;
            if (n != 0) {
                sb.append(" ");
            }
        }
        
        // This is the ones place
        if (n > 0) {
            sb.append(UNITS[n - 1]);
        }
        return sb.toString();
    }

plz help.how we convert negetive number into negetive world.for example-123 into negetive one hundred twenty three

hi can i see the code of number to words conversion
plzzzzzzzz
tnx aineed it asap

hi can i see the code of number to words conversion
plzzzzzzzz
tnx aineed it asap

What do you mean by that? If you can see it then what is the problem?

To >> ss999
This thread already has sample code for the conversion.

Here is a class file for converting Number to Word.
Download Class file

For example:

E:\Java_practical>java n2w
___________________________________
Enter an amount: 121
---------------------------------
The amount you entered in words is
one hundred and twenty one only
___________________________________

E:\Java_practical>java n2w
___________________________________
Enter an amount: -121
---------------------------------
The amount you entered in words is
Negative one hundred and twenty one only
___________________________________

E:\Java_practical>java n2w
___________________________________
Enter an amount: 2007483613
---------------------------------
The amount you entered in words is
two hundred crore seventy four lakh eighty three thousand six hundred and thirteen only
___________________________________

Input range: -2147483647 to 2147483647.
(without exception handling for 'string input' and 'input above the range').
I'm new for java. Here i used 'if else if', 'switch'& 'while'. Maybe some easy ways available for conversion.
The source code is about 241 lines with neat structure(My first java program:)).

Contact: <EMAIL SNIPPED>

Hi guys i am new here
i want to help about:
how to convert digit(number) into word
for example:
Enter a number:123
Output:one hundred twenty-three

i want to know how...can u tell me pls..how..?

Dude all the code is directly above, are you blind>?

Hi,
no need to hard code, just download bussinessapi.jar and add this jar file to your project. then write this code:

double array[] = { 100.22,13 }; 
AmountByWord amountByWord=new AmountByWord();
		amountByWord.setLanguage( "en" );
		amountByWord.setCurrency("IN");
for (int i = 0; i < array.length; i++)
		{
				
			try {
				System.out.println( amountByWord.getMoneyAmountByWord( array[ i ]  )  );
				
			} catch ( Exception e ) {
				e.printStackTrace();
			}
			
			System.out.println( "---------------------------------" );    	

		}

@naveen p prakas
1) You just replied to old thread
2) You did not use code tags
3) You recommend to use old deprecated API

what can I say BAD, BAD, BAD for all 3

Thread closed before another time traveller descent on us.

davidlad: what did you think peter_budo meant by the above post? this thread is five years old. just because some people seem to need to revive it now and then, doesn't mean it's the right thing to do.
the chances of the OP still looking for a solution for this are pretty small, wouldn't you agree?

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.