0

我应该从如下所示的文本文件中获取输入:

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();
            }

    }
}

但是现在的问题是这些年的哈希表是硬编码的,我想让这个程序动态地意味着无论输入(没有年份,排名或滚动没有),我希望我的输出对于任何输入都具有相同的格式。

4

2 回答 2

0

我认为这个任务是关于 3 件事:

  1. 将概念映射到适当的类层次结构
  2. 最重要的是,使用 compareTo 方法和 Collection 的不同 impl
  3. 一些样板文件,例如读取文件/输出

因此,您不应在一个主类中做所有事情,而应该

  • 编写一些映射这些概念的模型类
  • 在这些类中实现 compareTo 方法
  • 根据您的需要使用正确的集合实现(orderedlist?sortedset?)
  • 那么你的 main 方法应该解析文件、实例化对象并让 Java API 完成它的工作!
于 2012-10-06T16:59:53.710 回答
0

我做到了。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class Nu {

    public static void main(String[] args) throws FileNotFoundException{
        int lineCount=0;
        Scanner scn1=new Scanner(new File("D:/abc.txt"));
        while(scn1.hasNextLine()){
            scn1.nextLine();
            lineCount++;//count no of lines for array length
        }
        int count=1;
        int cnt=0;
        String[] yearss=new String[lineCount];//new array for years
        Scanner scn=new Scanner(new File("D:/abc.txt"));
        while(scn.hasNextLine()){//file yearss array
            Scanner sc=new Scanner(scn.nextLine());
            while(sc.hasNext()){
                if(count==1){
                    sc.next();
                    count++;
                }
                else if(count==2){
                    yearss[cnt]=sc.next();//yearss array is filled
                    count++;
                }
                else{
                    sc.next();
                    count=count-2;
                }
            }
            cnt++;
        }

        TreeSet<String> yr=new TreeSet<String>();//remove duplicate elements from years and sort them
        for(int i=1;i<yearss.length;i++)
            if(yearss[i]!=null)
                yr.add(yearss[i]);//add elements to treeset
        TreeMap allRecord=new TreeMap();//treemap sorts keys
        try{
            File file=new File("D:/abc.txt");//used for file searching
            String[] yearArray=(String[])yr.toArray(new String[0]);//convert treeset to String array
            String roll="";
            String year="";
            String rank="";
            BufferedReader in =new BufferedReader(new FileReader(file));//Filereader reads character by character and bufferedReader reads blocks or streams of data
            String value=in.readLine();//read next line 

            while(value!=null){
                StringTokenizer st=new StringTokenizer(value);//pass line into stringtokenizer
                roll=st.nextToken();//add 1st string to roll
                year=st.nextToken();//add 2nd to year
                rank=st.nextToken();//add third to rank
                TreeMap record=(allRecord.get(roll)!=null)?(TreeMap)allRecord.get(roll):null;//if roll != null then add roll to record else add null
                //returns year and rank//   System.out.println(allRecord.get(roll));
                if(record==null)//if null
                    record=new TreeMap();//if null create new instance
                    else
                    allRecord.remove(roll);//remove null from allrecord
                record.put(year, rank);//put year and rank//every time record is refreshed 
                allRecord.put(roll, record);//put 22 and record//treemap returns null if get(key)==null
                value=in.readLine();//read next line
            }

            Set keyset=allRecord.keySet();//gets all  keys from allRecord
            Iterator i=keyset.iterator();//iterate keyset
            System.out.print("rno"+" ");
            for(int j=0;j<yearArray.length;j++)
                System.out.print(yearArray[j]+"   ");
            //print all years
            System.out.println();
            while(i.hasNext()){
                roll=(String)i.next();//get roll numbers
                if(!roll.equals("rno"))
                System.out.print(roll+"    ");
                else
                    System.out.print("    ");
                Map record=(Map)allRecord.get(roll);//get record using the rollno
                for(int j=0;j<yearArray.length;j++){
                    rank=(String)record.get(yearArray[j]);
                    //get rank for the year
                    if(rank!=null){
                    System.out.print(rank+"    ");
                    }
                    else
                    {
                        System.out.print("       ");
                    }
                }
                System.out.println("");
            }
        }
        catch(Exception e){e.printStackTrace();}
    }
}*
于 2012-10-09T05:06:09.110 回答