Machine Problem

Create a program that converts numbers to words in Java programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com

Program Listing

/* Number_Words.java
 * Mr. Jake Rodriguez Pomperada,MAED-IT, MIT
 * www.jakerpomperada.com www.jakerpomperada.blogspot.com
 * jakerpomperada@gmail.com
 * Bacolod City, Negros Occidental Philippines
 */

import java.util.Scanner;

 public class Number_Words {
 
   public static void main(String[] args) {
	int number = 0;
	Scanner input = new Scanner(System.in);
	

    boolean valid;
    
    do {
    	 System.out.println();
    	 System.out.print("\tNumber To Words in Java\n\n");
    	 System.out.print("Enter Number: ");
	 
	   number = input.nextInt();
	   valid = (number >= 1   && number <= 100000);
       System.out.println();
           
       if (number > 100000) {
   		System.out.print("Number is out of range!\n");
   		
   	   } else if (!valid) {
       	System.out.println("Negative numbers, zero, and numbers greater than 100,000 are invalid input.");
         } 
       else {
		System.out.print(numberToWord(number));
	   }
		
   }while (!valid);
   System.out.println("\n");
  System.out.println("End of Program");
  input.close();
   }
 
   private static String numberToWord(int number) {
        
        String words = "";
        String unitsArray[] = { "zero", "one", "two", "three", "four", "five", "six", 
                      "seven", "eight", "nine", "ten", "eleven", "twelve",
                      "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", 
                      "eighteen", "nineteen" };
	String tensArray[] = { "zero", "ten", "twenty", "thirty", "forty", "fifty",
                      "sixty", "seventy", "eighty", "ninety" };
 
	if (number == 0) {
	    return "zero";
	}
	
	if (number < 0) { 
          
           String numberStr = "" + number; 
           
           numberStr = numberStr.substring(1); 
          
           return "minus " + numberToWord(Integer.parseInt(numberStr)); 
        } 
      
	
	if ((number / 1000) > 0) {
	    words += numberToWord(number / 1000) + " thousand ";
	    number %= 1000;
	}
	
	if ((number / 100) > 0) {
	     words += numberToWord(number / 100) + " hundred ";
	     number %= 100;
	}
 
	if (number > 0) {
	    
	     if (number < 20) { 
                    
                    words += unitsArray[number];
             } else { 
               
                words += tensArray[number / 10]; 
                if ((number % 10) > 0) {
		    words += "-" + unitsArray[number % 10];
                }  
	     }
          }
	  return words;
   }
}

Leave a Reply

Your email address will not be published.