Converting number into word, how??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cess
    New Member
    • Feb 2007
    • 19

    #1

    Converting number into word, how??

    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!
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by cess
    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.

    Comment

    • cess
      New Member
      • Feb 2007
      • 19

      #3
      Originally posted by r035198x
      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


      Code:
      import java.io.*;
        public class conversion
       {
          public static void main (String args[])throws Exception
        {
        	String number;
        	int amount;
        	
             System.out.print("Enter amount not exceed to two thousand: ");
              InputStreamReader no= new InputStreamReader(System.in);
           	BufferedReader num=new BufferedReader(no);
           	number=num.readLine();
           	amount=Integer.parseInt(number);
           
           if (amount>=2000)
           {
           	amount=amount-2000;
              System.out.println(number.replaceAll("2000","Two Thousand"));
           }
              if (amount>=1000)
              {
              amount=amount-1000;
           	System.out.println(number.replaceAll("1000","One Thousand"));
              }	
                 if (amount>=900)
                 {
           		amount=amount-900;
           		System.out.println(number.replaceAll("900","Nine Hundred"));
           	   }
                   if (amount>=800)
           		{
           		amount=amount-800;
           		System.out.println(number.replaceAll("800","Eight Hundred"));
           		}
           		   if (amount>=700)
             			{
           			amount=amount-700;
           			System.out.println(number.replaceAll("700","Seven Hundred"));
           			}
           				if (amount>=600)
                        	{
           				amount=amount-600;
           				System.out.println(number.replaceAll("600","Six Hundred"));
           				}
           				 if (amount>=500)
           				 {
           				 amount=amount-500;
           				 System.out.println(number.replaceAll("500","Five Hundred"));
           				 }
           				 	if (amount>=400)
              				{
           					amount=amount-400;
           					System.out.println(number.replaceAll("400","Four Hundred"));
           					}
           						if (amount>=300)
           						{
           						amount=amount-300;
           						System.out.println(number.replaceAll("300","Three Hundred"));
           						}
           							if (amount>=200)
           							{
           							amount=amount-200;
           							System.out.println(number.replaceAll("200","Two Hundred"));
           							}
         									if (amount>=100)
           								{
           								amount=amount-100;
           								System.out.println(number.replaceAll("100","One Hundred"));
           								}
                                     if (amount>=90)
                                     {
           	                       amount=amount-90;
           	                      System.out.println(number.replaceAll("90","Ninety"));
                                    }
                                if (amount>=80)
                                {
           	                  amount=amount-80;
           	                  System.out.println(number.replaceAll("80","Eighty"));
                                }
                              if (amount>=70)
                              {
           	                amount=amount-70;
                              System.out.println(number.replaceAll("70","Seventy"));
                              }
                          if (amount>=60)
                          {
                          amount=amount-60;
           	            System.out.println(number.replaceAll("60","Sixty"));
                          }
                     if (amount>=50)
                     {
                     amount=amount-50;
                     System.out.println(number.replaceAll("50","Fifty"));
                     }
                  if (amount>=40)
                  {
                  amount=amount-40;
                  System.out.println(number.replaceAll("40","Forty"));
                  }
               if (amount>=30)
               {
               amount=amount-30;
               System.out.println(number.replaceAll("30","Thirty"));
               }
            if (amount>=20)
            {
            amount=amount-20;
            System.out.println(number.replaceAll("20","Twenty"));
            }
            if (amount>=10)
            {
            amount=amount-10;
            System.out.println(number.replaceAll("10","Ten"));
            }
            if (amount>=9)
            {
            amount=amount-9;
            System.out.println(number.replaceAll("9","Nine"));
            }
            if (amount>=8)
            {
            amount=amount-8;
            System.out.println(number.replaceAll("8","Eight"));
            }
            if (amount>=7)
            {
            amount=amount-7;
            System.out.println(number.replaceAll("7","Seven"));
            }
                                    if (amount>=6)
                                     {
           	                       amount=amount-6;
           	                      System.out.println(number.replaceAll("6","Six"));
                                    }
                                    if (amount>=5)
                                     {
           	                       amount=amount-5;
           	                      System.out.println(number.replaceAll("6","Five"));
                                    }
                                    if (amount>=4)
                                     {
           	                       amount=amount-4;
           	                      System.out.println(number.replaceAll("4","Four"));
                                    }
                                    if (amount>=3)
                                     {
           	                       amount=amount-3;
           	                      System.out.println(number.replaceAll("3","Three"));
                                    }
                                    if (amount>=2)
                                     {
           	                       amount=amount-2;
           	                      System.out.println(number.replaceAll("2","Two"));
                                    }
                                    if (amount>=1)
                                     {
           	                       amount=amount-1;
           	                      System.out.println(number.replaceAll("1","One"));
                                    }
         }
        }

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        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.

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          Why did you not take the suggestion of using the % operator?

          Comment

          • cess
            New Member
            • Feb 2007
            • 19

            #6
            Originally posted by RedSon
            Why did you not take the suggestion of using the % operator?
            tnx for the comment!! I will try % operator..

            Comment

            • reon
              New Member
              • Feb 2007
              • 80

              #7
              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....

              Comment

              • reon
                New Member
                • Feb 2007
                • 80

                #8
                and one more thing you didnt entered zero

                Comment

                • chuwfan
                  New Member
                  • Mar 2007
                  • 9

                  #9
                  Originally posted by RedSon
                  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.

                  Comment

                  • RedSon
                    Recognized Expert Expert
                    • Jan 2007
                    • 4980

                    #10
                    Its call a modulus operator or mod operator, try googling for that instead.

                    Comment

                    • chuwfan
                      New Member
                      • Mar 2007
                      • 9

                      #11
                      Originally posted by RedSon
                      Its call a modulus operator or mod operator, try googling for that instead.

                      Thanks for the information, i will study it at google.

                      Comment

                      • SammyB
                        Recognized Expert Contributor
                        • Mar 2007
                        • 807

                        #12
                        Originally posted by chuwfan
                        Thanks for the information, i will study it at google.
                        You also want to use a hash table. Here it is in C#.
                        Code:
                        public static string NumberToWords(int num)
                        {
                        Hashtable htb = new Hashtable(); 
                        htb.Add(0, "no"); 
                        htb.Add(1, "one"); 
                        htb.Add(2, "two"); 
                        htb.Add(3, "three"); 
                        htb.Add(4, "four"); 
                        htb.Add(5, "five"); 
                        htb.Add(6, "six"); 
                        htb.Add(7, "seven"); 
                        htb.Add(8, "eight"); 
                        htb.Add(9, "nine"); 
                        htb.Add(10, "ten"); 
                        htb.Add(11, "eleven"); 
                        htb.Add(12, "twelve"); 
                        htb.Add(13, "thirteen"); 
                        htb.Add(14, "fourteen"); 
                        htb.Add(15, "fifteen"); 
                        htb.Add(16, "sixteen"); 
                        htb.Add(17, "seventeen"); 
                        htb.Add(18, "eighteen"); 
                        htb.Add(19, "nineteen"); 
                        htb.Add(20, "twenty"); 
                        htb.Add(30, "thirty"); 
                        htb.Add(40, "forty"); 
                        htb.Add(50, "fifty"); 
                        htb.Add(60, "sixty"); 
                        htb.Add(70, "seventy"); 
                        htb.Add(80, "eighty"); 
                        htb.Add(90, "ninety"); 
                        if (num<0 || num>99) return num.ToString();
                        if (num<20) return htb[num].ToString();
                        if (num%10 == 0) return htb[num].ToString();
                        return htb[(num/10)*10].ToString() + "-" + htb[num%10].ToString();
                        }
                        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.

                        Comment

                        Working...