Advertisement
f0rkB0mb

John Dave Rossel's Code

Oct 15th, 2013
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. import java.util.Scanner;
  2. class Convert
  3. {
  4.     public enum hundreds {OneHundred, TwoHundred, ThreeHundred, FourHundred, FiveHundred, SixHundred, SevenHundred, EightHundred, NineHundred}
  5.     public enum tens {Twenty, Thirty, Forty, Fifty, Sixty, Seventy, Eighty, Ninety}
  6.     public enum ones {One, Two, Three, Four, Five, Six, Seven, Eight, Nine}
  7.     public enum denom {Thousand, Million}
  8.     public enum splNums { Ten, Eleven, Twelve, Thirteen, Fourteen, Fifteen, Sixteen, Seventeen, Eighteen, Nineteen}
  9.     public static String text = "";
  10.     public static void main(String[] args)
  11.     {
  12.         System.out.println("Enter Number to convert into words");
  13.         Scanner sc = new Scanner(System.in);
  14.         long num = sc.nextInt();
  15.         int rem = 0;
  16.         int i = 0;
  17.         while(num > 0)
  18.         {
  19.             if(i == 0){
  20.                 rem = (int) (num % 1000);
  21.                 printText(rem);
  22.                 num = num / 1000;
  23.                 i++;
  24.             }
  25.             else if(num > 0)
  26.             {
  27.                 rem = (int) (num % 1000);
  28.                 if(rem > 0)
  29.                     text = denom.values()[i - 1]+ " " + text;
  30.                 printText(rem);
  31.                 num = num / 1000;
  32.                 i++;
  33.             }
  34.         }
  35.         if(i > 0)
  36.             System.out.println(text);
  37.         else
  38.             System.out.println("Zero");
  39.     }
  40.    
  41.     public static void printText(int num)
  42.     {
  43.         if(!(num > 9 && num < 19))
  44.         {
  45.             if(num % 10 > 0)
  46.                 getOnes(num % 10);
  47.             num = num / 10;
  48.             if(num % 10 > 0)
  49.                 getTens(num % 10);
  50.             num = num / 10;
  51.             if(num > 0)
  52.                 getHundreds(num);
  53.         }
  54.         else if (num > 9 && num < 19)
  55.         {
  56.             getSplNums(num % 10);
  57.         }
  58.     }
  59.     public static void getSplNums(int num)
  60.     {
  61.         text = splNums.values()[num]+ " " + text;
  62.     }
  63.     public static void getHundreds(int num)
  64.     {
  65.         text = hundreds.values()[num - 1]+ " " + text;
  66.     }
  67.     public static void getTens(int num)
  68.     {
  69.         text = tens.values()[num - 2]+ " " + text;
  70.     }
  71.     public static void getOnes(int num)
  72.     {
  73.         text = ones.values()[num - 1]+ " " + text;
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement