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 two three

Recommended Answers

All 21 Replies

There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in doing your homework for you.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

Start by working out the logic in ordinary English forget the Java for now.
Write down a few numbers, work out the correct answers, write doen how you did that. Then translate that into Java.

this program
solve by array or for loops

String a[] = {"Zero","One", "Two", "Three", "Four", "Five", "Six" , "Seven","Eight","Nine"};

That's a good start. Given the irregular pattern for the teens it nay be worth carrying on with that up to 19

and, depending on how far it must go, add logic for thousands and hundreds.
it's not at all difficut to implement, if you just want:
213
to be translated into
two one three
you can just print the elements of the array you already have by using the numbers-to-translate as indices.
if you need to go for more correct numbers, you'll have to add some conditional statements and different arrays, but it isn't much more difficult.

this program without hundred and thousand jusd red simple numbers . and this program too much difficult .also i try alot to solve ..
please help me .

import java.util.*;
class word_num
 {
  public static void main (String [] args)
    {
     int x,i;
     String m="\t";
     System.outprintln("Enter number");
     Scanner ip =new Scanner(System.in);
     for (i=1; i<=3; i++)
        {
        x=ip.nextIn();
        if(x==1)
        m=m+"one\t";
        else if (x==0)
        m=m+"zero\t";
        else if (x==2)
        m=m+"two\t";
        else if (x==3)
        m=m+"three\t";
        else if (x==4)
        m=m+"four\t";
        else if (x==5)
        m=m+"five\t";
        else if (x==6)
        m=m+"six\t";
        else if (x==7)
        m=m+"seven\t";
        else if (x==8)
        m=m+"eight\t";
        else if (x==9)
        m=m+"nine\t";
        }

        System.out.println("\n"+m);
     }
   }

ehm .. if you don't need to take into account hundreds/thousands, I 've told you in my previous reply how to solve it, using the array you have, and with a lot less code than this.

don't ignore answers, go through them and ask about them if you don't understand them.

thanks for answer
I am realy dont know how to use array in this program

import java.util.*;
class word_num
 {
  public static void main (String[] args)
    {
     int k,rev,k1;
     int b;
     Scanner sc=new Scanner(System.in);
     System.out.println("Enter any integer Numbers");
     k=sc.nextInt();
     k1=k;
     while(k!=0)
     {
     b=k%10; 
     rev=rev*10+b;
     k=k/10;
     }
     b=rev%10;
     switch(b)
     {
     case 0:
     System.out.println("zero"); break;
     case 1:
     System.out.println("one"); break;
     case 2:
     System.out.println("two"); break;
     case 3:
     System.out.println("three"); break;
     case 4:
     System.out.println("four"); break;
     case 5:
     System.out.println("five"); break;
     case 6:
     System.out.println("six"); break;
     case 7:
     System.out.println("seven"); break;
     case 8:
     System.out.println("eight"); break;
     case 9:
     System.out.println("nine"); break;
     }
     rev=rev/10;
     }
     }

The switch is OK, but the array was better. Here's how it works:
If you have your array as before

String a[] = {"Zero","One", "Two", "Three", "Four", "Five", "Six" , "Seven","Eight","Nine"};

Then
a[0] is "Zero"
a[1] is "One"
etc.
so if you have an int b with values 0-9 then a[b] is the corresponding string.

how I can int b with values 0-9 (i use array)

You already have it. That's exactly the same "b" that you use for your switch!

thanks
i understand this point ^^

i try this program

import java.util.*;
class word_num
 {
  public static void main (String[] args)
    {
     int b; 
     String a[] = {"Zero","One", "Two", "Three", "Four", "Five", "Six" , "Seven","Eight","Nine"};
     Scanner sc=new Scanner(System.in);
     System.out.println("Enter any integer Numbers");
     b=sc.nextInt();
     System.out.println(a[b]);

    } 
}

it is work , but it read just one number not more
like 7 7 seven
83 eight

In your previous (switch) program you had a loop that was doing /10 and %10 calculations to get each of the digits in turn. You still need a loop with something like that to get each of the digits.

like this

for(b=1; b<=10; b++)
     {
     b=b/10;
     b=b%10;
     }

like this

for(b=1; b<=10; b++)
     {
     b=b/10;
     b=b%10;
     }

No. Go back and look at your pervious code - I didn't read it all but it looks like it was doing the right kind of stuff

(ps - its dinner time here in France, I'm offline until tomorrow. Good luck. J)

thanks for all answers
have nice dinner ^_^

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.