473,503 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting number into word, how??

19 New Member
Frankly speaking, I have no code about this. I don't know how to start. This should be:

If the user inputs 1, the output is its corresponding word "one".

I need some help!
Mar 6 '07 #1
11 5553
r035198x
13,262 MVP
Frankly speaking, I have no code about this. I don't know how to start. This should be:

If the user inputs 1, the output is its corresponding word "one".

I need some help!
You need to have some code first to get code help. Write down your algorithm first. You may need to use the % operator.
Mar 6 '07 #2
cess
19 New Member
You need to have some code first to get code help. Write down your algorithm first. You may need to use the % operator.
I have now my code, but the problem is: if the user inputs "1500" the output is not "One Thousand Five Hundred", but "1500"and "1Five Hundred"...What is my lacking?? Here's the code


Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.   public class conversion
  3.  {
  4.     public static void main (String args[])throws Exception
  5.   {
  6.       String number;
  7.       int amount;
  8.  
  9.        System.out.print("Enter amount not exceed to two thousand: ");
  10.         InputStreamReader no= new InputStreamReader(System.in);
  11.          BufferedReader num=new BufferedReader(no);
  12.          number=num.readLine();
  13.          amount=Integer.parseInt(number);
  14.  
  15.      if (amount>=2000)
  16.      {
  17.          amount=amount-2000;
  18.         System.out.println(number.replaceAll("2000","Two Thousand"));
  19.      }
  20.         if (amount>=1000)
  21.         {
  22.         amount=amount-1000;
  23.          System.out.println(number.replaceAll("1000","One Thousand"));
  24.         }    
  25.            if (amount>=900)
  26.            {
  27.              amount=amount-900;
  28.              System.out.println(number.replaceAll("900","Nine Hundred"));
  29.             }
  30.              if (amount>=800)
  31.              {
  32.              amount=amount-800;
  33.              System.out.println(number.replaceAll("800","Eight Hundred"));
  34.              }
  35.                 if (amount>=700)
  36.                    {
  37.                  amount=amount-700;
  38.                  System.out.println(number.replaceAll("700","Seven Hundred"));
  39.                  }
  40.                      if (amount>=600)
  41.                       {
  42.                      amount=amount-600;
  43.                      System.out.println(number.replaceAll("600","Six Hundred"));
  44.                      }
  45.                       if (amount>=500)
  46.                       {
  47.                       amount=amount-500;
  48.                       System.out.println(number.replaceAll("500","Five Hundred"));
  49.                       }
  50.                           if (amount>=400)
  51.                         {
  52.                          amount=amount-400;
  53.                          System.out.println(number.replaceAll("400","Four Hundred"));
  54.                          }
  55.                              if (amount>=300)
  56.                              {
  57.                              amount=amount-300;
  58.                              System.out.println(number.replaceAll("300","Three Hundred"));
  59.                              }
  60.                                  if (amount>=200)
  61.                                  {
  62.                                  amount=amount-200;
  63.                                  System.out.println(number.replaceAll("200","Two Hundred"));
  64.                                  }
  65.                                        if (amount>=100)
  66.                                      {
  67.                                      amount=amount-100;
  68.                                      System.out.println(number.replaceAll("100","One Hundred"));
  69.                                      }
  70.                                if (amount>=90)
  71.                                {
  72.                                 amount=amount-90;
  73.                                System.out.println(number.replaceAll("90","Ninety"));
  74.                               }
  75.                           if (amount>=80)
  76.                           {
  77.                            amount=amount-80;
  78.                            System.out.println(number.replaceAll("80","Eighty"));
  79.                           }
  80.                         if (amount>=70)
  81.                         {
  82.                          amount=amount-70;
  83.                         System.out.println(number.replaceAll("70","Seventy"));
  84.                         }
  85.                     if (amount>=60)
  86.                     {
  87.                     amount=amount-60;
  88.                      System.out.println(number.replaceAll("60","Sixty"));
  89.                     }
  90.                if (amount>=50)
  91.                {
  92.                amount=amount-50;
  93.                System.out.println(number.replaceAll("50","Fifty"));
  94.                }
  95.             if (amount>=40)
  96.             {
  97.             amount=amount-40;
  98.             System.out.println(number.replaceAll("40","Forty"));
  99.             }
  100.          if (amount>=30)
  101.          {
  102.          amount=amount-30;
  103.          System.out.println(number.replaceAll("30","Thirty"));
  104.          }
  105.       if (amount>=20)
  106.       {
  107.       amount=amount-20;
  108.       System.out.println(number.replaceAll("20","Twenty"));
  109.       }
  110.       if (amount>=10)
  111.       {
  112.       amount=amount-10;
  113.       System.out.println(number.replaceAll("10","Ten"));
  114.       }
  115.       if (amount>=9)
  116.       {
  117.       amount=amount-9;
  118.       System.out.println(number.replaceAll("9","Nine"));
  119.       }
  120.       if (amount>=8)
  121.       {
  122.       amount=amount-8;
  123.       System.out.println(number.replaceAll("8","Eight"));
  124.       }
  125.       if (amount>=7)
  126.       {
  127.       amount=amount-7;
  128.       System.out.println(number.replaceAll("7","Seven"));
  129.       }
  130.                               if (amount>=6)
  131.                                {
  132.                                 amount=amount-6;
  133.                                System.out.println(number.replaceAll("6","Six"));
  134.                               }
  135.                               if (amount>=5)
  136.                                {
  137.                                 amount=amount-5;
  138.                                System.out.println(number.replaceAll("6","Five"));
  139.                               }
  140.                               if (amount>=4)
  141.                                {
  142.                                 amount=amount-4;
  143.                                System.out.println(number.replaceAll("4","Four"));
  144.                               }
  145.                               if (amount>=3)
  146.                                {
  147.                                 amount=amount-3;
  148.                                System.out.println(number.replaceAll("3","Three"));
  149.                               }
  150.                               if (amount>=2)
  151.                                {
  152.                                 amount=amount-2;
  153.                                System.out.println(number.replaceAll("2","Two"));
  154.                               }
  155.                               if (amount>=1)
  156.                                {
  157.                                 amount=amount-1;
  158.                                System.out.println(number.replaceAll("1","One"));
  159.                               }
  160.    }
  161.   }       
Mar 21 '07 #3
RedSon
5,000 Recognized Expert Expert
Woah this code is a nightmare! The problem you are having is because your code is unreadable, it is also going to be very difficult to maintain, what if I was to input 3000 or 4000 or even 10000. You must think of a much more elegant solution to this problem then massive ifs like you have. When your design is better your problem will fix itself.
Mar 21 '07 #4
RedSon
5,000 Recognized Expert Expert
Why did you not take the suggestion of using the % operator?
Mar 21 '07 #5
cess
19 New Member
Why did you not take the suggestion of using the % operator?
tnx for the comment!! I will try % operator..
Mar 22 '07 #6
reon
80 New Member
HI cess

Your idea is great...
But you didnt specify 11 to 19...
I am a newbie in java .. the only thing i found inside that code is that....
Mar 23 '07 #7
reon
80 New Member
and one more thing you didnt entered zero
Mar 23 '07 #8
chuwfan
9 New Member
Why did you not take the suggestion of using the % operator?
Sorry cause i a newbie in java, maybe this is a stupid question,
but may i know what is the % operator or may i know where can get this information?
i try to search at google.com but i can't really get what it means at all.

Thanks.
Mar 23 '07 #9
RedSon
5,000 Recognized Expert Expert
Its call a modulus operator or mod operator, try googling for that instead.
Mar 23 '07 #10
chuwfan
9 New Member
Its call a modulus operator or mod operator, try googling for that instead.

Thanks for the information, i will study it at google.
Mar 24 '07 #11
SammyB
807 Recognized Expert Contributor
Thanks for the information, i will study it at google.
You also want to use a hash table. Here it is in C#.
Expand|Select|Wrap|Line Numbers
  1. public static string NumberToWords(int num)
  2. {
  3. Hashtable htb = new Hashtable(); 
  4. htb.Add(0, "no"); 
  5. htb.Add(1, "one"); 
  6. htb.Add(2, "two"); 
  7. htb.Add(3, "three"); 
  8. htb.Add(4, "four"); 
  9. htb.Add(5, "five"); 
  10. htb.Add(6, "six"); 
  11. htb.Add(7, "seven"); 
  12. htb.Add(8, "eight"); 
  13. htb.Add(9, "nine"); 
  14. htb.Add(10, "ten"); 
  15. htb.Add(11, "eleven"); 
  16. htb.Add(12, "twelve"); 
  17. htb.Add(13, "thirteen"); 
  18. htb.Add(14, "fourteen"); 
  19. htb.Add(15, "fifteen"); 
  20. htb.Add(16, "sixteen"); 
  21. htb.Add(17, "seventeen"); 
  22. htb.Add(18, "eighteen"); 
  23. htb.Add(19, "nineteen"); 
  24. htb.Add(20, "twenty"); 
  25. htb.Add(30, "thirty"); 
  26. htb.Add(40, "forty"); 
  27. htb.Add(50, "fifty"); 
  28. htb.Add(60, "sixty"); 
  29. htb.Add(70, "seventy"); 
  30. htb.Add(80, "eighty"); 
  31. htb.Add(90, "ninety"); 
  32. if (num<0 || num>99) return num.ToString();
  33. if (num<20) return htb[num].ToString();
  34. if (num%10 == 0) return htb[num].ToString();
  35. return htb[(num/10)*10].ToString() + "-" + htb[num%10].ToString();
  36. }
This is only for 0-99 because that's all i needed, but it's easy to extend. Just add hundred and thousand to the hash and do a few more DIV' and MOD's. But, sorry, I don't do Java.
Mar 24 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1895
by: Dan Stromberg | last post by:
I've written up a page about how to convert native binary data to another platform's native binary data, as I did some fortran data conversions for a client. The programs and documentation are...
20
7298
by: Al Moritz | last post by:
Hi all, I was always told that the conversion of Word files to HTML as done by Word itself sucks - you get a lot of unnecessary code that can influence the design on web browsers other than...
11
3191
by: Chris Online | last post by:
Hi all, I'm using C++ Builder5. I want to get data from an edit-box and send it to a development kit. The dev-kit can only receive char and no char* here's a part of my code: char* Data_byte...
8
5714
by: prabha | last post by:
Hello Everybody, I have to conert the word doc to multiple html files,according to the templates in the word doc. I had converted the word to xml.Also through Exsl ,had finished the multiple...
2
5740
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option...
7
3029
by: Converting Numeric to a word | last post by:
Hi, I have little experience in C#, Can anyone help me with this sproblem. " I want to convert a given number in a text to a word on a lable" for eg if we type 25 in text box , i want to display it...
12
1773
by: AMP | last post by:
Hello, I have in c: WORD calcChecksum(BYTE data, WORD length) { WORD* i_data; WORD checksum= 0; BYTE i= 0; i_data= (WORD*)data;
7
4453
anukagni
by: anukagni | last post by:
Hi friends, I want to get this thing happen.. I want to convert the number to word i.e. if i entry 1 in an text box then it should give the reflect to One in an lable .. Is this possible...
1
1768
by: danmilkman | last post by:
Hi Y'all Can someone help me convert the next few lines of code from c++ to c#? I've given it a try myself but my program keeps crashing when I use my c# code. I think the difficulty lies in...
1
1493
by: ganesh22 | last post by:
Hi, Iam getting the below error while my application is running on IIS. in my application iam converting a text into word format, so i added some .dll from COM for converting word format ...
0
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5566
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.