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

やったよ。

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 に答える
0

この課題は、次の 3 つのことだと思います。

  1. 概念をクラスの適切な階層にマッピングする
  2. 最も重要なのは、compareTo メソッドと Collection の異なる impl を使用することです。
  3. ファイル/出力の読み取りなどの定型的なもの

したがって、すべてを 1 つの Main クラスで行うのではなく、

  • それらの概念をマッピングするいくつかのモデル クラスを記述します。
  • それらのクラスにcompareToメソッドを実装する
  • 必要に応じて適切なコレクションの実装を使用してください (orderedlist? sortedset?)
  • 次に、メイン メソッドでファイルを解析し、オブジェクトをインスタンス化し、Java API にその作業を任せる必要があります。
于 2012-10-06T16:59:53.710 に答える