日期:2014-05-20  浏览次数:20708 次

数字转换问题
文件out.txt中,有如下内容:

239、567、345
540/137/879
 247//839//693
658/369/147
247//十:389//个:369
4679---4679---4679
3469*0479*3689
345679 、 013568 、 52980371

。。。。。。。。。。。。。。多条数据
要求:
编写程序,从out.txt中读取内容,经过处理后,显示
效果(每行一条数据)如下:

0145678、0123489、0126789
1236789、0245689、0123456
0135689、0124567、0124578
0123479、0124578、0235689
0135689、0124567、0124578
012358、012358、012358
012578、123568、012457
0128、2479、46

注:相当于将每条数据中,每部分数字换成没有出现的数字(0~9)
如: 345679 、 013568 、 52980371 这条数据
经过处理后,变成:
0128、2479、46 这条数据。



 

------解决方案--------------------
上面那个还有bug,这个
ChangeNumbersNew.java
Java code

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ChangeNumbersNew {
    
    static List<String> strList = new ArrayList<String>();
    
    public static void main(String[] args) {
        File f = new File("out.txt");
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(f);
            br = new BufferedReader(fr);
            String lineStr = "";
            String s = "";
            while((lineStr = br.readLine()) != null){
                lineStr = lineStr.replaceAll(" ", "");
                for(int i=0;i<lineStr.length();i++){
                    if(isNumber(lineStr.charAt(i))){
                        s += String.valueOf(lineStr.charAt(i));
                    }else{
                        if(s != ""){
                            strList.add(s);
                            s = "";
                        }
                    }
                    if(i == lineStr.length()-1){
                        strList.add(s);
                        s = "";
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                if(br != null){
                    br.close();
                    br = null;
                }
                if(fr != null){
                    fr.close();
                    fr = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String outLine = "";
        for(int i = 1;i <= strList.size();i++){
            outLine += (getNumbers(strList.get(i-1)))+"、";
            if(i%3 == 0){
                if(outLine.endsWith("、")){
                    outLine = outLine.substring(0,outLine.length()-1);
                }
                System.out.println(outLine);
                outLine = "";
            }
        }
    }
    static boolean isNumber(char c){
        if(c >= '0' && c <= '9'){
            return true;
        }
        return false;
    }
    static String getNumbers(String s){
        List<String> numList = new ArrayList<String>();
        for(int i = 0;i<10;i++){
            numList.add(String.valueOf(i));
        }
        for(int i = 0;i<s.length();i++){
            char c = s.charAt(i);
            for(int j = 0;j<numList.size();j++){
                if(numList.get(j).equals(String.valueOf(c))){
                    numList.remove(j);
                }
            }
        }
        String retVal = "";
        for(String str:numList){
            retVal += str;
        }
        return retVal;
    }
}