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

字符串转化问题
txt里的内容是:
111:02-98781999:Williams:Nick:T:35:Computer Officer:14-10-2000
131:02-95673456:Couch:David:A:26:Consultant:23-04-1994
553:03-99999999:Coles:David:M:12:Manager:12-12-1999
148:02-93272658:Smith:John:C:43:Technical Manager:21-10-1988
372:02-12345678:Miller:Sam:B:22:Engineer:12-03-1998
059:02-95673455:Chen:Xiao:Y:26:Consultant:01-05-2003
对他进行格式化,也就是按工号排序,输出的内容是这样:
059 02-.........
111 02-98781999 Williams .......
........
........

------解决方案--------------------
你预想的怎样输出......
------解决方案--------------------
Java code

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;


public class StringTest {

    public static void main(String[] args)throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("1.txt")));
        TreeMap<Integer,String> maps = new TreeMap<Integer,String>();
        String line = null;
        while((line=in.readLine())!=null){
            int i = line.indexOf(":");
            String str1 = line.substring(0,i);
            int key = Integer.parseInt(str1);
            String str2 = line.substring(i+1);
            maps.put(key, str2);
        }
        Set<Map.Entry<Integer, String>> set = maps.entrySet();
        for(Map.Entry<Integer, String> entry:set){
            String str = entry.getValue();
            String[]s = str.split(":");
            System.out.print(entry.getKey()+" ");
            for(String e:s){
                System.out.print(e+" ");
            }
            System.out.println("\n");
        }
    }

}