Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Number to Word Converter 2.0 - Java Code Bank


Number to Word Converter 2.0
This is another approach at the number to word converter. This time I took use of char and pointers from the ascii table. ArrayList to make a dynamic array, added a boolean for negative numbers.
                package word_converter_fn;
import java.util.ArrayList;

/**
 * @author elmiguel
 */

public class word_converter_fn21 {
   private static String[] ones     = {"zero","one","two","three","four","five","six","seven","eight","nine"}; 
   private static String[] elevens  = {"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}; 
   private static String[] tens     = {"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
   private static String[] grouping = {"hundred", "thousand", "million", "billion", "trillion", "quadrillion", "sextillion", "septillion", "octillion","nonillion", "decillion"}; 

   // Return the char digit character as is literal counter part (pointer)
   public static String GetDigit(String[] type, char dc){
       if (type == tens) dc -= 2; // correct the index for ten. ;p 
       return type[dc - 48].toString().trim();
   }  
   
   public static String ProcessDouble(char one, char two){
       String temp = (one == '1') ? GetDigit(elevens, one) : GetDigit(tens, one) + " " + GetDigit(ones, two);
       // if out ends with "zero", trim it out. thank you, thank you ;p
       if (temp.endsWith("zero")) temp = temp.substring(0, temp.length()-4);
       return temp.trim();
   }
   
   public static String ProcessTriple(char one, char two, char three, int set){     
       String num = (one == '0') ? "" /*Awesome: zero hundred!*/ : GetDigit(ones, one) + " " + grouping[0];
       String num2 = (two == '0') ? GetDigit(ones, three) : ProcessDouble(two, three);
       String suffix = grouping[set];
       String result = num + " " + num2 + " " + suffix;
       return result.trim();
   }

   public static String prePad(String n, int pad){
       String padding = "";
       switch(pad){
           case 0:break;//has three numbers
           case 1:padding = "00"; break;//only one digit
           case 2:padding = "0"; break; //only two digits
       }     
       return padding + n;
   }
   
   public static String Process(ArrayList<String> numbers){
       String result = "";
       //Create a new array, since ArrayList sucks with iterations
       String[] nums = new String[numbers.size()];       
       for (int i=0; i<=numbers.size()-1; i++) nums[i] = numbers.get(i);    

       for (int i = 0;i <= nums.length-1; i++){ 
           result += " " + ProcessTriple(nums[i].charAt(0),nums[i].charAt(1),nums[i].charAt(2), (nums.length-1)-i);      
       }
       // return without the hundred suffix at the end. then trim it
       return result.substring(0,result.length()-8).trim();           
   }
  
   public static String convert(String n){       
       String out = "";
       int len = n.length();
       // Single digit, return number
       if (len == 1) return GetDigit(ones, n.charAt(0)); 
       
       // double digits, check for ten or elevens
       if (len == 2){ 
           if (n.charAt(0) == '1'){// "this amplifier goes to eleven!"
               return GetDigit(elevens, n.charAt(1));                
           } else {
               //not and eleven; return number
               out = ProcessDouble(n.charAt(0), n.charAt(1));
           }
       }
    
       // 3 or more digits, process groups and finalize output
       if (len >= 3){
          // correct the number to add the right amount of sets
          n = prePad(n, n.length()%3);
          
          //Create a new ArrayList<String> (dynamic) since normal java Arrays suck at dynamics
          //use (n.length()-1) instead of len because we padded the number
          ArrayList<String> sets = new ArrayList<String>();
          for(int i=0; i <= (n.length()-1)/3; i++){ 
              sets.add(n.substring(i*3, (i*3)+3));        
          }      
         out = Process(sets);
       }
       return out.trim();
   }
   
   public static void main(String[] args){
        for(String arg : args){
           if (arg != null || !arg.equals("")){                
               String input = arg;
               //Check or negative numbers
                boolean sign = false;
                if (arg.startsWith("-")){
                    sign = true;
                    arg = arg.substring(1);
                }
               String output = (sign)? "negative " + convert(arg.trim()) : convert(arg.trim());
               
               System.out.println("input : " + input + "\r\nOutput : " + output);
           }
       }      
   }
}

            
Comments
Sorry but there are no comments to display