Number To Words in Java

Language
This tutorial will teach you how to create a program that will convert a number and will become words in java. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of NumberToWords.java. 2. Import the io library as we will need a user input.
  1. import java.io.*;
3. Now, we will declare our global variables. We will have variables for the inputting number, ones,tens, elevens, hundreds, thousands, and the zero. We will have only a maximum input of 3000.
  1.         static int n,thou,hun,tens,ones,zero,elevens;
We will create the BufferedReader class with variable in, and n as intger that will make as our input.
  1.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  2.        
  3.         System.out.println("Enter Number: ");
  4.     n = Integer.parseInt (in.readLine());
4. We will code first for inputting of the zero. We will just use the if and switch statement here. Have this code below:
  1.                 if (n==0){
  2.                        
  3.                         switch (n){
  4.                                  case 0: System.out.println("Zero");break;
  5.                                
  6.                                 }
5. Now, this code will be for the hundred and thousand when inputting 3 to 4 digits. We will use the '%' for the modulus as we have to get the zeroes and the remainder. Have this code below:
  1.                 if ((n>=100) && (n<=3000)){
  2.                        
  3.                         thou = n/1000;
  4.                         n = n%1000;
  5.                         hun = n/100;
  6.                         n = n%100;
  7.                        
  8.                         switch(thou){
  9.                                
  10.                                 case 1:System.out.print("One Thousand ");break;
  11.                                 case 2:System.out.print("Two Thousand ");break;
  12.                                 case 3:System.out.print("Three Thousand ");break;              
  13.                         }
  14.                        
  15.                        
  16.                         switch (hun) {
  17.                                 case 1:System.out.print("One Hundred ");break;
  18.                                 case 2:System.out.print("Two Hundred ");break;
  19.                                 case 3:System.out.print("Three Hundred ");break;
  20.                                 case 4:System.out.print ("Four Hundred ");break;
  21.                                 case 5:System.out.print ("Five Hundred ");break;       
  22.                                 case 6:System.out.print ("Six Hundred ");break;
  23.                                 case 7:System.out.print("Seven Hundred ");break;
  24.                                 case 8:System.out.print ("Eight Hundred ");break;
  25.                                 case 9:System.out.print ("Nine Hundred ");break;
  26.                                
  27.                                                        
  28.                         }
  29.                 }
  30.                
6. For the eleven to nineteen, we will filter such code with the if condition followed by the switch statement.
  1.         if ((n>10) && (n<20)){
  2.                
  3.                 tens = n / 10;
  4.                 ones = n;
  5.                 elevens = n % 10;
  6.                
  7.                
  8.                 switch (elevens){
  9.                                 case 1:System.out.print("Eleven ");break;
  10.                                 case 2:System.out.print("Twelve ");break;
  11.                                 case 3:System.out.print("Thirteen ");break;
  12.                                 case 4:System.out.print ("Fourteen ");break;
  13.                                 case 5:System.out.print ("Fifteen ");break;    
  14.                                 case 6:System.out.print ("Sixteen ");break;
  15.                                 case 7:System.out.print("Seventeen ");break;
  16.                                 case 8:System.out.print ("Eighteen ");break;
  17.                                 case 9:System.out.print ("Nineteen ");break;
  18.                                 }
  19.                 }
7. Then have this code for the ones and tens.
  1.                
  2.         else {
  3.                 tens = n/10;
  4.                 n = n%10;
  5.                 ones = n;
  6.                
  7.                 switch (tens) {
  8.                        
  9.                         case 1: System.out.print("Ten ");break;
  10.                         case 2:System.out.print("Twenty ");break;
  11.                         case 3:System.out.print("Thirty ");break;
  12.                
  13.                         case 4:System.out.print("Fourty ");break;
  14.                         case 5:System.out.print("Fifty ");break;
  15.                         case 6:System.out.print("Sixty ");break;
  16.                         case 7:System.out.print("Seventy ");break;
  17.                         case 8:System.out.print("Eighty ");break;
  18.                         case 9: System.out.print("Ninety ");break;                     
  19.                 }
  20.                 switch (ones){
  21.                        
  22.                         case 1: System.out.print("One ");break;
  23.                         case 2:System.out.print("Two ");break;
  24.                         case 3:System.out.print("Three ");break;
  25.                
  26.                         case 4:System.out.print("Four ");break;
  27.                         case 5:System.out.print("Five ");break;
  28.                         case 6:System.out.print("Six ");break;
  29.                         case 7:System.out.print("Seven ");break;
  30.                         case 8:System.out.print("Eight ");break;
  31.                         case 9:System.out.print("Nine ");break;
  32.                        
  33.                 }
  34.        
  35.         }
8. Remember that our conversion will have only its maximum number of 3000, so we must filter it. Have this code:
  1.                 if (n>3000){
  2.                         System.out.print("INVALID INPUT");
  3.                 }
Output: output Here's the full code of this tutorial:
  1. import java.io.*;
  2. public class NumberToWords {
  3.  
  4.         static int n,thou,hun,tens,ones,zero,elevens;
  5.        
  6.         public static void main(String args[]) throws IOException{
  7.        
  8.        
  9.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  10.        
  11.         System.out.println("Enter Number: ");
  12.     n = Integer.parseInt (in.readLine());
  13.                
  14.                 if (n==0){
  15.                        
  16.                         switch (n){
  17.                                  case 0: System.out.println("Zero");break;
  18.                                
  19.                                 }
  20.                                 }
  21.        
  22.                 if ((n>=100) && (n<=3000)){
  23.                        
  24.                         thou = n/1000;
  25.                         n = n%1000;
  26.                         hun = n/100;
  27.                         n = n%100;
  28.                        
  29.                         switch(thou){
  30.                                
  31.                                 case 1:System.out.print("One Thousand ");break;
  32.                                 case 2:System.out.print("Two Thousand ");break;
  33.                                 case 3:System.out.print("Three Thousand ");break;              
  34.                         }
  35.                        
  36.                        
  37.                         switch (hun) {
  38.                                 case 1:System.out.print("One Hundred ");break;
  39.                                 case 2:System.out.print("Two Hundred ");break;
  40.                                 case 3:System.out.print("Three Hundred ");break;
  41.                                 case 4:System.out.print ("Four Hundred ");break;
  42.                                 case 5:System.out.print ("Five Hundred ");break;       
  43.                                 case 6:System.out.print ("Six Hundred ");break;
  44.                                 case 7:System.out.print("Seven Hundred ");break;
  45.                                 case 8:System.out.print ("Eight Hundred ");break;
  46.                                 case 9:System.out.print ("Nine Hundred ");break;
  47.                                
  48.                                                        
  49.                         }
  50.                 }
  51.                
  52.                                        
  53.         if ((n>10) && (n<20)){
  54.                
  55.                 tens = n / 10;
  56.                 ones = n;
  57.                 elevens = n % 10;
  58.                
  59.                
  60.                 switch (elevens){
  61.                                 case 1:System.out.print("Eleven ");break;
  62.                                 case 2:System.out.print("Twelve ");break;
  63.                                 case 3:System.out.print("Thirteen ");break;
  64.                                 case 4:System.out.print ("Fourteen ");break;
  65.                                 case 5:System.out.print ("Fifteen ");break;    
  66.                                 case 6:System.out.print ("Sixteen ");break;
  67.                                 case 7:System.out.print("Seventeen ");break;
  68.                                 case 8:System.out.print ("Eighteen ");break;
  69.                                 case 9:System.out.print ("Nineteen ");break;
  70.                                 }
  71.                 }
  72.                 if (n>3000){
  73.                         System.out.print("INVALID INPUT");
  74.                 }
  75.                
  76.         else {
  77.                 tens = n/10;
  78.                 n = n%10;
  79.                 ones = n;
  80.                
  81.                 switch (tens) {
  82.                        
  83.                         case 1: System.out.print("Ten ");break;
  84.                         case 2:System.out.print("Twenty ");break;
  85.                         case 3:System.out.print("Thirty ");break;
  86.                
  87.                         case 4:System.out.print("Fourty ");break;
  88.                         case 5:System.out.print("Fifty ");break;
  89.                         case 6:System.out.print("Sixty ");break;
  90.                         case 7:System.out.print("Seventy ");break;
  91.                         case 8:System.out.print("Eighty ");break;
  92.                         case 9: System.out.print("Ninety ");break;                     
  93.                 }
  94.                 switch (ones){
  95.                        
  96.                         case 1: System.out.print("One ");break;
  97.                         case 2:System.out.print("Two ");break;
  98.                         case 3:System.out.print("Three ");break;
  99.                
  100.                         case 4:System.out.print("Four ");break;
  101.                         case 5:System.out.print("Five ");break;
  102.                         case 6:System.out.print("Six ");break;
  103.                         case 7:System.out.print("Seven ");break;
  104.                         case 8:System.out.print("Eight ");break;
  105.                         case 9:System.out.print("Nine ");break;
  106.                        
  107.                 }
  108.        
  109.         }
  110.                
  111. }  
  112. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Add new comment