码迷,mamicode.com
首页 > 其他好文 > 详细

Change Number to English By Reading rule of money

时间:2014-08-14 01:30:57      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:collection   java   

In the partime,  a simle program attracted my attention whose content is to change number to english by reading rule of money.It took about one hour to deal with this question. Now the source was shared with everyone. 

[java] view plaincopy
  1. import java.io.BufferedReader;  
  2. import java.io.BufferedWriter;  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileWriter;  
  7. import java.io.IOException;  
  8. import java.io.InputStreamReader;  
  9. import java.io.UnsupportedEncodingException;  
  10.   
  11. public class Number {  
  12.   
  13.     public static final String[] ENGLISH_NUMBER = { "zero""one""two",  
  14.             "three""four""five""six""seven""eight""nine" };  
  15.   
  16.     public static final String[] ENGLISH_DOCNUMBER = { "ten""twenty",  
  17.             "thirty""fourty""fifty""sixty""seventy""eighty""ninety" };  
  18.   
  19.     public static final String[] ENGLISH_UNIT = { "hundred""thousand",  
  20.             "million""billion" };  
  21.   
  22.     public static final StringBuilder sb = new StringBuilder(0);  
  23.   
  24.     public static void parse() {  
  25.         sb.setLength(0);  
  26.         readFile("in.txt");  
  27.         writeFile(sb.toString(), "out.txt");  
  28.   
  29.         return;  
  30.     }  
  31.   
  32.     public static void readFile(String fileName) {  
  33.   
  34.         File file = new File(fileName);  
  35.         FileInputStream fis = null;  
  36.         BufferedReader br = null;  
  37.         try {  
  38.             fis = new FileInputStream(file);  
  39.             br = new BufferedReader(new InputStreamReader(fis, "gb2312"));  
  40.             String temp = "";  
  41.             while ((temp = br.readLine()) != null) {  
  42.                 sb.append(temp).append("=").append(formate(parseTotal(temp)))  
  43.                         .append("\r\n");  
  44.             }  
  45.         } catch (FileNotFoundException e) {  
  46.             e.printStackTrace();  
  47.         } catch (UnsupportedEncodingException e) {  
  48.             e.printStackTrace();  
  49.         } catch (IOException e) {  
  50.             e.printStackTrace();  
  51.         } finally {  
  52.             try {  
  53.                 if (br != null) {  
  54.                     br.close();  
  55.                 }  
  56.                 if (fis != null) {  
  57.                     fis.close();  
  58.                 }  
  59.             } catch (IOException e) {  
  60.                 e.printStackTrace();  
  61.             }  
  62.         }  
  63.     }  
  64.   
  65.     public static void writeFile(String result, String fileName) {  
  66.         File f = new File(fileName);  
  67.         FileWriter fw = null;  
  68.         BufferedWriter bw = null;  
  69.   
  70.         try {  
  71.             if (!f.exists()) {  
  72.                 f.createNewFile();  
  73.             }  
  74.             fw = new FileWriter(f);  
  75.             bw = new BufferedWriter(fw);  
  76.             bw.write(result);  
  77.         } catch (Exception e) {  
  78.             e.printStackTrace();  
  79.         } finally {  
  80.             try {  
  81.                 if (bw != null) {  
  82.                     bw.close();  
  83.                 }  
  84.             } catch (IOException e) {  
  85.                 e.printStackTrace();  
  86.             }  
  87.         }  
  88.     }  
  89.   
  90.     public static String parseLine(String str) {  
  91.         String result = "";  
  92.         if (isLegel(str)) {  
  93.             while (str.length() > 0) {  
  94.                 int length = str.length();  
  95.                 switch (length) {  
  96.                 case 1:  
  97.                     result += ENGLISH_NUMBER[Integer.valueOf(str)];  
  98.                     str = "";  
  99.                     break;  
  100.                 case 2:  
  101.                     char[] strArr = str.toCharArray();  
  102.                     if (Character.getNumericValue(strArr[0]) == 0) {  
  103.                         result += ("zero" + ENGLISH_NUMBER[Character  
  104.                                 .getNumericValue(strArr[1])]);  
  105.                     } else {  
  106.                         result += (ENGLISH_DOCNUMBER[Character  
  107.                                 .getNumericValue(strArr[0]) - 1]  
  108.                                 + " " + ENGLISH_NUMBER[Character  
  109.                                 .getNumericValue(strArr[1])]);  
  110.                     }  
  111.                     str = "";  
  112.                     break;  
  113.                 case 3:  
  114.                     char[] strArr1 = str.toCharArray();  
  115.                     result += ENGLISH_NUMBER[Character  
  116.                             .getNumericValue(strArr1[0])]  
  117.                             + " " + ENGLISH_UNIT[0] + " and ";  
  118.                     str = str.substring(1);  
  119.                     break;  
  120.                 case 4:  
  121.                     char[] strArr2 = str.toCharArray();  
  122.                     result += ENGLISH_NUMBER[Character  
  123.                             .getNumericValue(strArr2[0])]  
  124.                             + " " + ENGLISH_UNIT[1] + " and ";  
  125.                     str = str.substring(1);  
  126.                     break;  
  127.                 default:  
  128.                     break;  
  129.                 }  
  130.             }  
  131.         } else {  
  132.             result = "error";  
  133.         }  
  134.   
  135.         if (result.indexOf("zero") != -1) {  
  136.             result = result.replace("zero""");  
  137.         }  
  138.         if (result.trim().endsWith("and")) {  
  139.             result = result.substring(0, result.lastIndexOf("and"));  
  140.         }  
  141.         return result.trim();  
  142.     }  
  143.   
  144.     public static String parseTotal(String str) {  
  145.         String result = "";  
  146.         while (str.length() > 0) {  
  147.             if(str.indexOf("error") != -1)  
  148.             {  
  149.                 result = "error";  
  150.                 break;  
  151.             }  
  152.             if (str.length() <= 3) {  
  153.                 result += parseLine(str);  
  154.                 break;  
  155.             } else if (str.length() > 3 && str.length() < 7) {  
  156.                 String highStr = str.substring(0, str.length() - 3);  
  157.                 String lowStr = str.substring(str.length() - 3);  
  158.                 result += (parseLine(highStr) + " " + ENGLISH_UNIT[1] + " " + parseLine(lowStr));  
  159.                 break;  
  160.             } else if (str.length() >= 7 && str.length() <= 9) {  
  161.                 String highStr = str.substring(0, str.length() - 6);  
  162.                 result += (parseLine(highStr) + " " + ENGLISH_UNIT[2] + " ");  
  163.                 str = str.substring(str.length() - 5);  
  164.             } else if (str.length() > 9 && str.length() <= 10) {  
  165.                 String highStr = str.substring(0, str.length() - 9);  
  166.                 result = parseLine(highStr) + " " + ENGLISH_UNIT[3] + " ";  
  167.                 str = str.substring(str.length() - 8);  
  168.             }  
  169.         }  
  170.   
  171.         // 111,112,112  
  172.         return result.toLowerCase();  
  173.     }  
  174.   
  175.     public static boolean isLegel(String str) {  
  176.         boolean flag = true;  
  177.         if (str.length() >= 10) {  
  178.             flag = false;  
  179.         } else if (str.indexOf(".") != -1) {  
  180.             flag = false;  
  181.         } else {  
  182.             try {  
  183.                 int number = Integer.parseInt(str);  
  184.                 if (number <= 0) {  
  185.                     flag = false;  
  186.                 }  
  187.             } catch (NumberFormatException e) {  
  188.                 flag = false;  
  189.             }  
  190.         }  
  191.   
  192.         return flag;  
  193.     }  
  194.   
  195.     public static String formate(String str) {  
  196.         str = str.replace("ten one""eleven");  
  197.         str = str.replace("ten two""twelve");  
  198.         str = str.replace("ten three""thirteen");  
  199.         str = str.replace("ten four""fourteen");  
  200.         str = str.replace("ten five""fifteen");  
  201.         str = str.replace("ten six""sixteen");  
  202.         str = str.replace("ten seven""seventeen");  
  203.         str = str.replace("ten eight""eighteen");  
  204.         str = str.replace("ten nine""nineteen");  
  205.   
  206.         return str;  
  207.     }  
  208.   
  209.     public static void main(String[] args) {  
  210.         System.out.println(formate(parseTotal("11632")));  
  211.         System.out.println(formate(parseTotal("123")));  
  212.     }  
  213. }  

The output is: 

eleven thousand six hundred and thirty two
one hundred and twenty three


Change Number to English By Reading rule of money,布布扣,bubuko.com

Change Number to English By Reading rule of money

标签:collection   java   

原文地址:http://blog.csdn.net/sxb0841901116/article/details/38548865

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!