我应该从如下所示的文本文件中获取输入:
rno year rank
22 2004 12
18 2004 17
43 2005 15
43 2006 45
18 2005 23
22 2005 15
43 2004 16
22 2006 20
我应该对这些进行排序并按以下顺序生成输出:
rno  2004  2005  2006
18    17     23        
22    12     15    20    
43    16     15    45  
如果卷号没有可用的等级。对于相应的年份,然后打印一个空格
我能够使用以下代码做到这一点:
public class Test {
    public static void main(String args[]) throws FileNotFoundException{
        String[] rollNo=new String[20];
        String[] year=new String[20];
        String[] rank=new String[20];
        int count=1;
        int cnt=0;
        Scanner scn=new Scanner(new File("D:/abc.txt"));
        while(scn.hasNextLine()){
            Scanner sc=new Scanner(scn.nextLine());
                while(sc.hasNext()){
                if(count==1){
                    rollNo[cnt]=sc.next();
                    count++;
                }
                else if(count==2){
                    year[cnt]=sc.next();
                    count++;
                }
                else{
                    rank[cnt]=sc.next();
                    count=count-2;
                }
            }
            cnt++;
        }
            TreeSet<String> yr=new TreeSet<String>();
            for(int i=1;i<year.length;i++)
            if(year[i]!=null)
            yr.add(year[i]);
            TreeSet<String> rl=new TreeSet<String>();
            for(int i=1;i<rollNo.length;i++)
            if(rollNo[i]!=null)
            rl.add(rollNo[i]);
            Hashtable<String,String> y1=new Hashtable<String,String>();
            for(int ct=0;ct<year.length;ct++)
                if(year[ct]!=null)
                    if(year[ct].equals("2004"))
                        y1.put(rollNo[ct], rank[ct]);
            Hashtable<String,String> y2=new Hashtable<String,String>();
            for(int ct=0;ct<year.length;ct++)
                if(year[ct]!=null)
                    if(year[ct].equals("2005"))
                        y2.put(rollNo[ct], rank[ct]);
            Hashtable<String,String> y3=new Hashtable<String,String>();
            for(int ct=0;ct<year.length;ct++)
                if(year[ct]!=null)
                    if(year[ct].equals("2006"))
                        y3.put(rollNo[ct], rank[ct]);
            System.out.print(rollNo[0]);
            for(Object obj:yr)
                System.out.print("  "+obj);
                    System.out.println();
            for(Object obj:rl){
                System.out.print(obj+"   ");
            if(y1.containsKey(obj))
                System.out.print(y1.get(obj)+"    ");
            else
                System.out.print(" ");
            if(y2.containsKey(obj))
                System.out.print(y2.get(obj)+"    ");
            else
                System.out.print("    ");
            if(y3.containsKey(obj))
                System.out.print(y3.get(obj)+"    ");
            else
                System.out.print("    ");
            System.out.println();
            }
    }
}
但是现在的问题是这些年的哈希表是硬编码的,我想让这个程序动态地意味着无论输入(没有年份,排名或滚动没有),我希望我的输出对于任何输入都具有相同的格式。