正则表达式的使用

哎呀呀第一次写博客,有点小激动啊~~,整理一下之前写过的代码吧

用正则表达式实现了一个将阿拉伯数字转化为中文读法的功能.只支持到千万
import java.io.*;

/**
 * Created by Administrator on 2016/8/5.
 */
public class MynumTrans {
    public static void main(String[] args) {
        try(
                FileOutputStream fileOutputStream=new FileOutputStream("result.txt");
                FileInputStream fileInputStream=new FileInputStream("text.txt");
                OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutputStream,"UTF-8");
                InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream,"UTF-8");
                BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
                BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);

        )
        {
            String str;
            int percent=0;
            while ((str=bufferedReader.readLine())!=null){
                bufferedWriter.write(transwan(Integer.parseInt(str)));
                bufferedWriter.newLine();
                Thread.sleep(100);
                percent++;
                System.out.printf("\r[");
                for (int i = 0; i <percent ; i++) {
                    System.out.printf("=");
                }
                System.out.printf(">>]");
                System.out.printf("\t%.2f%%", (float)percent);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }



    public static String transwan(int num){
        if(num>=100000000){
            return "数太大了";
        }
        StringBuilder stringBuilder=new StringBuilder();
        if(num>=10000){
            stringBuilder.append(transqian(num/10000)).append("万").append(transqian(num%10000));

        }else{//如果是400000 则不进入条件,否则会出现四十万零
            stringBuilder.append(transqian(num));
        }
        if(stringBuilder.length()>1){
            if (stringBuilder.lastIndexOf("零")==stringBuilder.length()-1){
                stringBuilder.deleteCharAt(stringBuilder.length()-1);
            }
        }
        return stringBuilder.toString();
    }


    public static String transqian(int num){
        String[] shu={"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append(shu[num/1000]).append("千").append(shu[num/100%10]).append("百")
                .append(shu[num/10%10]).append("十").append(shu[num%10]);
        //除去零后面的单位
       int index=-1;
        while ((index=stringBuilder.indexOf("零",index+1))!=-1){
            if (index<stringBuilder.length()-1){
                stringBuilder.deleteCharAt(index+1);
            }
        }
        //除去连续的两个零其中的一个
        index=0;
        while ((index=stringBuilder.indexOf("零零",index))!=-1){
            if(index<stringBuilder.length()){
                stringBuilder.deleteCharAt(index);
            }
        }
        //如果零不是唯一的一个数,除去最后面和最前面的零
        if (stringBuilder.length()>1){
            if (stringBuilder.lastIndexOf("零")==stringBuilder.length()-1){
                stringBuilder.deleteCharAt(stringBuilder.length()-1);
            }
            if (stringBuilder.indexOf("零")==0){
                stringBuilder.deleteCharAt(0);
            }
        }
        if (stringBuilder.indexOf("一十")==1){
            stringBuilder.deleteCharAt(0);
        }

        return stringBuilder.toString();
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值