473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Conver number to words (thousands) help

Hello,

I already have the program ( convert number to words but only 0-999 ). I need it to be ( 0-9999 ).

can you help me?

Expand|Select|Wrap|Line Numbers
  1. package num2word;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class NumbertoWords {
  6.  
  7.     // units Method
  8.     public static void units(int n) {
  9.  
  10.         switch (n) {
  11.             case 1:
  12.                 System.out.print("one ");
  13.                 break;
  14.             case 2:
  15.                 System.out.print("two ");
  16.                 break;
  17.             case 3:
  18.                 System.out.print("three ");
  19.                 break;
  20.             case 4:
  21.                 System.out.print("four ");
  22.                 break;
  23.             case 5:
  24.                 System.out.print("five ");
  25.                 break;
  26.             case 6:
  27.                 System.out.print("six ");
  28.                 break;
  29.             case 7:
  30.                 System.out.print("seven ");
  31.                 break;
  32.             case 8:
  33.                 System.out.print("eight ");
  34.                 break;
  35.             case 9:
  36.                 System.out.print("nine ");
  37.                 break;
  38.         }
  39.     }
  40.  
  41.     // special case Method
  42.     public static void special(int n) {
  43.  
  44.         switch (n) {
  45.             case 11:
  46.                 System.out.print(" eleven");
  47.                 break;
  48.             case 12:
  49.                 System.out.print(" twelve");
  50.                 break;
  51.             case 13:
  52.                 System.out.print(" thirteen");
  53.                 break;
  54.             case 14:
  55.                 System.out.print(" fourteen");
  56.                 break;
  57.             case 15:
  58.                 System.out.print(" fifteen");
  59.                 break;
  60.             case 16:
  61.                 System.out.print(" sixteen");
  62.                 break;
  63.             case 17:
  64.                 System.out.print(" seventeen");
  65.                 break;
  66.             case 18:
  67.                 System.out.print(" eighteen");
  68.                 break;
  69.             case 19:
  70.                 System.out.print(" nineteen");
  71.                 break;
  72.         }
  73.     }
  74.  
  75.     // tens Method
  76.     public static void tens(int n) {
  77.  
  78.         switch (n) {
  79.             case 1:
  80.                 System.out.print(" ten ");
  81.             case 2:
  82.                 System.out.print(" twenty ");
  83.                 break;
  84.             case 3:
  85.                 System.out.print(" thirty ");
  86.                 break;
  87.             case 4:
  88.                 System.out.print(" forty ");
  89.                 break;
  90.             case 5:
  91.                 System.out.print(" fifty ");
  92.                 break;
  93.             case 6:
  94.                 System.out.print(" sixty ");
  95.                 break;
  96.             case 7:
  97.                 System.out.print(" seventy ");
  98.                 break;
  99.             case 8:
  100.                 System.out.print(" eighty ");
  101.                 break;
  102.             case 9:
  103.                 System.out.print(" ninety ");
  104.                 break;
  105.         }
  106.     }
  107.  
  108.     // hundreds Method
  109.     public static void hundreds(int n) {
  110.  
  111.         switch (n) {
  112.             case 1:
  113.                 System.out.print("one hundred ");
  114.                 break;
  115.             case 2:
  116.                 System.out.print("two hundred ");
  117.                 break;
  118.             case 3:
  119.                 System.out.print("three hundred ");
  120.                 break;
  121.             case 4:
  122.                 System.out.print("four hundred ");
  123.                 break;
  124.             case 5:
  125.                 System.out.print("five hundred ");
  126.                 break;
  127.             case 6:
  128.                 System.out.print("six hundred ");
  129.                 break;
  130.             case 7:
  131.                 System.out.print("seven hundred ");
  132.                 break;
  133.             case 8:
  134.                 System.out.print("eight hundred ");
  135.                 break;
  136.             case 9:
  137.                 System.out.print("nine hundred ");
  138.                 break;
  139.         }
  140.     }
  141.  
  142.     public static void main(String[] args) {
  143.         Scanner input = new Scanner(System.in);
  144.  
  145.         // Requesting User input
  146.         System.out.print("Enter an integer number[0 to 999]::>> ");
  147.         int number = input.nextInt();
  148.  
  149.         // input validation
  150.         while (number < 0 || number > 999) {
  151.             System.out.println("Input Too Large, ");
  152.             System.out.print("Enter an integer number[0 to 999]::>> ");
  153.             number = input.nextInt();
  154.         }
  155.  
  156.         // Below we start <strong class="highlight">to</strong> determine what the input was by
  157.         // categorising it using boolean
  158.  
  159.         // original number (100-999)
  160.         if (number > 99 && number < 1000) {
  161.             int h = number / 100;    //find the hundreds only (one, two hundred)
  162.             hundreds(h);             //print number
  163.  
  164.             // first part of number found & printed (hundreds)
  165.             // remamining 2 digits must be found ie tens & units
  166.  
  167.             // test if they are a special number
  168.             int x = 0;               // initialized variable x for calculations
  169.             x = number % 100;        // find remainder of hundreds (ie x54)
  170.             if (x > 10 && x < 20) {  // ie is 54 a special number?
  171.                 special(x);          // print number
  172.             }
  173.             // if not special number -> split up into tens & units
  174.             if (x > 0 && x < 100) {
  175.  
  176.                 int tens = x / 10;   // finding the tens (forty, fifty)
  177.                 tens(tens);          // print number
  178.  
  179.                 int units = x % 10;  // finding the units (one, two)
  180.                 units(units);        // print number
  181.             }
  182.         }
  183.  
  184.         // original number (20-99)
  185.         if (number > 19 && number < 100) {
  186.  
  187.             int t = number / 10;     // finding the tens (forty, fifty)
  188.             tens(t);                 // print number
  189.  
  190.             int u = number % 10;     // finding the units (one, two)
  191.             units(u);                // print number
  192.         }
  193.  
  194.         // original number (11-19) (Special <strong class="highlight">numbers</strong>)
  195.         if (number > 10 && number < 20) { // (Special <strong class="highlight">numbers</strong>)
  196.             special(number);         // print number
  197.         }
  198.  
  199.         // original number (0-9)
  200.         if (number < 10) {           // finding the units (one, two)
  201.             units(number);           // print number
  202.         }
  203.     }
  204. }
Thanks
Mar 20 '10 #1

✓ answered by jkmyoung

First off, split the cases up smarter. eg pseudocode:
Expand|Select|Wrap|Line Numbers
  1. Currently you have:
  2. if (thousands){
  3.   print thousands
  4.   print hundreds
  5.   print special
  6.   print tens
  7.   print ones
  8. } else {
  9.   print hundreds
  10.   print special
  11.   print tens
  12.   print ones 
  13. }
  14.  
  15. Whereas, to simplify the code, you really should have:
  16. If (thousands?)
  17.   Print thousands
  18. if (hundreds?)
  19.   Print hundreds
  20. if (special?){
  21.   print special
  22. } else {
  23.   print tens
  24.   print ones
  25. }
ALSO!! You could simply add a case 0:
case 0: // output nothing
break;

2. the way you're getting your tens digit is off.
int tens = x / 10;
should be
int tens = x/10 % 10;

You can do this for all of your number parts. eg
thousands = number / 1000 % 10;
hundreds = number / 100 % 10;
tens = number / 10 % 10;
units = number % 10;

4 19761
Here's the updated program:
Expand|Select|Wrap|Line Numbers
  1. package num2word;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class NumbertoWords {
  6.  
  7.     // units Method
  8.     public static void units(int n) {
  9.  
  10.         switch (n) {
  11.             case 1:
  12.                 System.out.print("one ");
  13.                 break;
  14.             case 2:
  15.                 System.out.print("two ");
  16.                 break;
  17.             case 3:
  18.                 System.out.print("three ");
  19.                 break;
  20.             case 4:
  21.                 System.out.print("four ");
  22.                 break;
  23.             case 5:
  24.                 System.out.print("five ");
  25.                 break;
  26.             case 6:
  27.                 System.out.print("six ");
  28.                 break;
  29.             case 7:
  30.                 System.out.print("seven ");
  31.                 break;
  32.             case 8:
  33.                 System.out.print("eight ");
  34.                 break;
  35.             case 9:
  36.                 System.out.print("nine ");
  37.                 break;
  38.         }
  39.     }
  40.  
  41.     // special case Method
  42.     public static void special(int n) {
  43.  
  44.         switch (n) {
  45.             case 11:
  46.                 System.out.print(" eleven");
  47.                 break;
  48.             case 12:
  49.                 System.out.print(" twelve");
  50.                 break;
  51.             case 13:
  52.                 System.out.print(" thirteen");
  53.                 break;
  54.             case 14:
  55.                 System.out.print(" fourteen");
  56.                 break;
  57.             case 15:
  58.                 System.out.print(" fifteen");
  59.                 break;
  60.             case 16:
  61.                 System.out.print(" sixteen");
  62.                 break;
  63.             case 17:
  64.                 System.out.print(" seventeen");
  65.                 break;
  66.             case 18:
  67.                 System.out.print(" eighteen");
  68.                 break;
  69.             case 19:
  70.                 System.out.print(" nineteen");
  71.                 break;
  72.         }
  73.     }
  74.  
  75.     // tens Method
  76.     public static void tens(int n) {
  77.  
  78.         switch (n) {
  79.             case 1:
  80.                 System.out.print(" ten ");
  81.             case 2:
  82.                 System.out.print(" twenty ");
  83.                 break;
  84.             case 3:
  85.                 System.out.print(" thirty ");
  86.                 break;
  87.             case 4:
  88.                 System.out.print(" forty ");
  89.                 break;
  90.             case 5:
  91.                 System.out.print(" fifty ");
  92.                 break;
  93.             case 6:
  94.                 System.out.print(" sixty ");
  95.                 break;
  96.             case 7:
  97.                 System.out.print(" seventy ");
  98.                 break;
  99.             case 8:
  100.                 System.out.print(" eighty ");
  101.                 break;
  102.             case 9:
  103.                 System.out.print(" ninety ");
  104.                 break;
  105.         }
  106.     }
  107.  
  108.     // hundreds Method
  109.     public static void hundreds(int n) {
  110.  
  111.         switch (n) {
  112.             case 1:
  113.                 System.out.print("one hundred ");
  114.                 break;
  115.             case 2:
  116.                 System.out.print("two hundred ");
  117.                 break;
  118.             case 3:
  119.                 System.out.print("three hundred ");
  120.                 break;
  121.             case 4:
  122.                 System.out.print("four hundred ");
  123.                 break;
  124.             case 5:
  125.                 System.out.print("five hundred ");
  126.                 break;
  127.             case 6:
  128.                 System.out.print("six hundred ");
  129.                 break;
  130.             case 7:
  131.                 System.out.print("seven hundred ");
  132.                 break;
  133.             case 8:
  134.                 System.out.print("eight hundred ");
  135.                 break;
  136.             case 9:
  137.                 System.out.print("nine hundred ");
  138.                 break;
  139.         }
  140.     }
  141.     // hundreds Method
  142.     public static void thousands(int n) {
  143.  
  144.         switch (n) {
  145.             case 1:
  146.                 System.out.print("one thousand ");
  147.                 break;
  148.             case 2:
  149.                 System.out.print("two thousand ");
  150.                 break;
  151.             case 3:
  152.                 System.out.print("three thousand ");
  153.                 break;
  154.             case 4:
  155.                 System.out.print("four thousand ");
  156.                 break;
  157.             case 5:
  158.                 System.out.print("five thousand ");
  159.                 break;
  160.             case 6:
  161.                 System.out.print("six thousand ");
  162.                 break;
  163.             case 7:
  164.                 System.out.print("seven thousand ");
  165.                 break;
  166.             case 8:
  167.                 System.out.print("eight thousand ");
  168.                 break;
  169.             case 9:
  170.                 System.out.print("nine thousand ");
  171.                 break;
  172.         }
  173.     }
  174.     public static void main(String[] args) {
  175.         Scanner input = new Scanner(System.in);
  176.  
  177.         // Requesting User input
  178.         System.out.print("Enter an integer number[0 to 9999]::>> ");
  179.         int number = input.nextInt();
  180.  
  181.         // input validation
  182.         while (number < 0 || number > 9999) {
  183.             System.out.println("Input Too Large, ");
  184.             System.out.print("Enter an integer number[0 to 9999]::>> ");
  185.             number = input.nextInt();
  186.         }
  187.  
  188.         // Below we start <strong class="highlight">to</strong> determine what the input was by
  189.         // categorising it using boolean
  190.  
  191. // thousands (1000-9999)
  192.         if (number > 999 && number < 10000) {
  193.             int t = number / 1000;    //find the hundreds only (one, two hundred)
  194.             thousands(t);             //print number
  195.  
  196.  
  197.             // first part of number found & printed (hundreds)
  198.             // remamining 2 digits must be found ie tens & units
  199.  
  200.             // test if they are a special number
  201.             int x = 0;               // initialized variable x for calculations
  202.             x = number % 1000;        // find remainder of hundreds (ie x54)
  203.             if (x > 10 && x < 20) {  // ie is 54 a special number?
  204.                 special(x);          // print number
  205.             }
  206.             // if not special number -> split up into tens & units
  207.             if (x > 0 && x < 1000) {
  208.  
  209.                 int hundreds = x / 100;   // finding the hundreds
  210.                 hundreds(hundreds);          // print number
  211.  
  212.                 int tens = x / 10;   // finding the tens (forty, fifty)
  213.                 tens(tens);          // print number
  214.  
  215.                 int units = x % 10;  // finding the units (one, two)
  216.                 units(units);        // print number
  217.             }
  218.         }
  219.         // original number (100-999)
  220.         if (number > 99 && number < 1000) {
  221.             int h = number / 100;    //find the hundreds only (one, two hundred)
  222.             hundreds(h);             //print number
  223.  
  224.  
  225.             // first part of number found & printed (hundreds)
  226.             // remamining 2 digits must be found ie tens & units
  227.  
  228.             // test if they are a special number
  229.             int x = 0;               // initialized variable x for calculations
  230.             x = number % 100;        // find remainder of hundreds (ie x54)
  231.             if (x > 10 && x < 20) {  // ie is 54 a special number?
  232.                 special(x);          // print number
  233.             }
  234.             // if not special number -> split up into tens & units
  235.             if (x > 0 && x < 100) {
  236.  
  237.                 int tens = x / 10;   // finding the tens (forty, fifty)
  238.                 tens(tens);          // print number
  239.  
  240.                 int units = x % 10;  // finding the units (one, two)
  241.                 units(units);        // print number
  242.             }
  243.         }
  244.  
  245.         // original number (20-99)
  246.         if (number > 19 && number < 100) {
  247.  
  248.             int t = number / 10;     // finding the tens (forty, fifty)
  249.             tens(t);                 // print number
  250.  
  251.             int u = number % 10;     // finding the units (one, two)
  252.             units(u);                // print number
  253.         }
  254.  
  255.         // original number (11-19) (Special <strong class="highlight">numbers</strong>)
  256.         if (number > 10 && number < 20) { // (Special <strong class="highlight">numbers</strong>)
  257.             special(number);         // print number
  258.         }
  259.  
  260.         // original number (0-9)
  261.         if (number < 10) {           // finding the units (one, two)
  262.             units(number);           // print number
  263.         }
  264.     }
  265. }
The output is:
Expand|Select|Wrap|Line Numbers
  1. Enter an integer number[0 to 999]::>> 5670
  2. five thousand six hundred 
  3. Process completed.
  4.  
I dont know where the problem is.
help please?

thanks!
Mar 21 '10 #2
sukatoa
539 512MB
I guess it would be simpler if you convert the input value into a string. access the element of that string and get the representation....
Mar 21 '10 #3
like this one?
Expand|Select|Wrap|Line Numbers
  1. public class NumberToWords {
  2.  
  3.     private static final String[] ONES = {
  4.         "Zero", "One", "Two", "Three", "Four", "Five",
  5.         "Six", "Seven", "Eight", "Nine" };
  6.     private static final String[] TEENS = {
  7.         "Ten", "Eleven", "Twelve", "Thirteen", null, "Fifteen",
  8.         null, null, "Eighteen", null };
  9.     private static final String[] TENS = {
  10.         null, null, "Twenty", "Thirty", "Forty", "Fifty",
  11.         "Sixty", "Seventy", "Eighty", "Ninety" };
  12.  
  13.     public static String numberToWords(int number) {
  14.         if (number<10) {
  15.             return ONES[number];
  16.         } else if (number<20) {
  17.             int n = number - 10;
  18.             String words = TEENS[n];
  19.             return words==null ? ONES[n]+"teen" : TEENS[n];
  20.         } else {
  21.             int n = number % 10;
  22.             return TENS[number/10] +
  23.                 (n==0 ? "" : (" " + numberToWords(n)));
  24.         }
  25.     }
  26.  
  27.     public static void main(String[] args) {
  28.         for (int i=0; i<100; i++) {
  29.             System.out.println(i+" "+numberToWords(i));
  30.         }
  31.     }
  32. }
  33.  
Mar 21 '10 #4
jkmyoung
2,057 Expert 2GB
First off, split the cases up smarter. eg pseudocode:
Expand|Select|Wrap|Line Numbers
  1. Currently you have:
  2. if (thousands){
  3.   print thousands
  4.   print hundreds
  5.   print special
  6.   print tens
  7.   print ones
  8. } else {
  9.   print hundreds
  10.   print special
  11.   print tens
  12.   print ones 
  13. }
  14.  
  15. Whereas, to simplify the code, you really should have:
  16. If (thousands?)
  17.   Print thousands
  18. if (hundreds?)
  19.   Print hundreds
  20. if (special?){
  21.   print special
  22. } else {
  23.   print tens
  24.   print ones
  25. }
ALSO!! You could simply add a case 0:
case 0: // output nothing
break;

2. the way you're getting your tens digit is off.
int tens = x / 10;
should be
int tens = x/10 % 10;

You can do this for all of your number parts. eg
thousands = number / 1000 % 10;
hundreds = number / 100 % 10;
tens = number / 10 % 10;
units = number % 10;
Mar 22 '10 #5

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

Similar topics

3
by: david | last post by:
HI! Im trying to make "HTML form" into automatic. 1. If I get 18 numbers like: A B C D E F . . . . 2. How can I put those 18 numbers automatically into 6 numbers format like: A B C D E F
16
by: Douglas | last post by:
Gday, How would I format a number so that: TheValue = 32500 Displays in the TextBox as: $32,500.00
3
by: vanvee | last post by:
Hi I have an application for my company's HR department where we store resumes for candidates we receive. I have an application that uses VB.Net and ADO.Net and data bindings (through code) to...
1
by: oviam packirisamy | last post by:
how to convert number to string in sqlserver eg:- 10 -> ten display *** Sent via Developersdex http://www.developersdex.com ***
3
by: Ntl News Group | last post by:
I have a text box that contains the number 1234, I want the value form the text box to display in a second text box so I used: = The problems is that this displays 1234 and I want it to display...
4
by: mustang07 | last post by:
I need this program to find all occurences of a name and display them. If I enter Palmer, I need it to display all the Palmers in the list. Can anyone help me Please. Thank You!! #include...
7
by: Gonzchris | last post by:
What would be the easiest way of going about converting a 3 digit hexadecimal number to decimal, I think that a switch statement is out of the question due to multiple digits. Would it be easiest...
3
by: malmus | last post by:
<%# DataBinder.Eval(Container.DataItem, "ShortText")%> I have the code above in C# and i want to display only the first 100 words. How can i do that??
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.