首页 > 代码库 > Change Number to English By Reading rule of money
Change Number to English By Reading rule of money
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.
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; public class Number { public static final String[] ENGLISH_NUMBER = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; public static final String[] ENGLISH_DOCNUMBER = { "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" }; public static final String[] ENGLISH_UNIT = { "hundred", "thousand", "million", "billion" }; public static final StringBuilder sb = new StringBuilder(0); public static void parse() { sb.setLength(0); readFile("in.txt"); writeFile(sb.toString(), "out.txt"); return; } public static void readFile(String fileName) { File file = new File(fileName); FileInputStream fis = null; BufferedReader br = null; try { fis = new FileInputStream(file); br = new BufferedReader(new InputStreamReader(fis, "gb2312")); String temp = ""; while ((temp = br.readLine()) != null) { sb.append(temp).append("=").append(formate(parseTotal(temp))) .append("\r\n"); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } if (fis != null) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } } public static void writeFile(String result, String fileName) { File f = new File(fileName); FileWriter fw = null; BufferedWriter bw = null; try { if (!f.exists()) { f.createNewFile(); } fw = new FileWriter(f); bw = new BufferedWriter(fw); bw.write(result); } catch (Exception e) { e.printStackTrace(); } finally { try { if (bw != null) { bw.close(); } } catch (IOException e) { e.printStackTrace(); } } } public static String parseLine(String str) { String result = ""; if (isLegel(str)) { while (str.length() > 0) { int length = str.length(); switch (length) { case 1: result += ENGLISH_NUMBER[Integer.valueOf(str)]; str = ""; break; case 2: char[] strArr = str.toCharArray(); if (Character.getNumericValue(strArr[0]) == 0) { result += ("zero" + ENGLISH_NUMBER[Character .getNumericValue(strArr[1])]); } else { result += (ENGLISH_DOCNUMBER[Character .getNumericValue(strArr[0]) - 1] + " " + ENGLISH_NUMBER[Character .getNumericValue(strArr[1])]); } str = ""; break; case 3: char[] strArr1 = str.toCharArray(); result += ENGLISH_NUMBER[Character .getNumericValue(strArr1[0])] + " " + ENGLISH_UNIT[0] + " and "; str = str.substring(1); break; case 4: char[] strArr2 = str.toCharArray(); result += ENGLISH_NUMBER[Character .getNumericValue(strArr2[0])] + " " + ENGLISH_UNIT[1] + " and "; str = str.substring(1); break; default: break; } } } else { result = "error"; } if (result.indexOf("zero") != -1) { result = result.replace("zero", ""); } if (result.trim().endsWith("and")) { result = result.substring(0, result.lastIndexOf("and")); } return result.trim(); } public static String parseTotal(String str) { String result = ""; while (str.length() > 0) { if(str.indexOf("error") != -1) { result = "error"; break; } if (str.length() <= 3) { result += parseLine(str); break; } else if (str.length() > 3 && str.length() < 7) { String highStr = str.substring(0, str.length() - 3); String lowStr = str.substring(str.length() - 3); result += (parseLine(highStr) + " " + ENGLISH_UNIT[1] + " " + parseLine(lowStr)); break; } else if (str.length() >= 7 && str.length() <= 9) { String highStr = str.substring(0, str.length() - 6); result += (parseLine(highStr) + " " + ENGLISH_UNIT[2] + " "); str = str.substring(str.length() - 5); } else if (str.length() > 9 && str.length() <= 10) { String highStr = str.substring(0, str.length() - 9); result = parseLine(highStr) + " " + ENGLISH_UNIT[3] + " "; str = str.substring(str.length() - 8); } } // 111,112,112 return result.toLowerCase(); } public static boolean isLegel(String str) { boolean flag = true; if (str.length() >= 10) { flag = false; } else if (str.indexOf(".") != -1) { flag = false; } else { try { int number = Integer.parseInt(str); if (number <= 0) { flag = false; } } catch (NumberFormatException e) { flag = false; } } return flag; } public static String formate(String str) { str = str.replace("ten one", "eleven"); str = str.replace("ten two", "twelve"); str = str.replace("ten three", "thirteen"); str = str.replace("ten four", "fourteen"); str = str.replace("ten five", "fifteen"); str = str.replace("ten six", "sixteen"); str = str.replace("ten seven", "seventeen"); str = str.replace("ten eight", "eighteen"); str = str.replace("ten nine", "nineteen"); return str; } public static void main(String[] args) { System.out.println(formate(parseTotal("11632"))); System.out.println(formate(parseTotal("123"))); } }
The output is:
eleven thousand six hundred and thirty two
one hundred and twenty three
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。