我应该从文本文件中获取输入,如下所示:
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();
}
}
}
但现在的问题是这些年的哈希表是硬编码的,我想让这个程序动态地意味着无论输入什么(没有年份,排名或滚动否),我希望我的输出以相同的格式输入任何输入
答案 0 :(得分:0)
我认为这项任务大约有三件事:
因此,不应该在一个Main类中执行所有操作,而应该
答案 1 :(得分: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();}
}
}*