texfile的初始输出是这个。 不允许任何阵列或比较者:
Steve Jobs 9 f 91
Bill Gates 6 m 90
James Gosling 3 m 100
James Gosling 3 f 100
Dennis Ritchie 5 m 94
Steve Jobs 9 m 95
Dennis Ritchie 5 f 100
Jeff Dean 7 m 100
Bill Gates 6 f 96
Jeff Dean 7 f 100
Sergey Brin 27 f 97
Sergey Brin 22 m 98
collateExams 方法从第一个' m'开始整理/排序检查对象。 (期中)第一个对象,紧接着是同一个人的(最终)。只允许 SINGLE循环构造。 collateExams()的输出应该是下面的输出,但我的代码不起作用,即collateExams方法不起作用。 smb可以帮助我吗? collateExams()的输出应为
Bill Gates 6 m 90
Bill Gates 6 f 96
James Gosling 3 m 100
James Gosling 3 f 100
Dennis Ritchie 5 m 94
Dennis Ritchie 5 f 100
Steve Jobs 9 m 95
Steve Jobs 9 f 91
Jeff Dean 7 m 100
Jeff Dean 7 f 100
Sergey Brin 22 m 98
Sergey Brin 27 f 97
我在
获得NullExceptionsr[2*position[exams[i].getID()]+1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
import java.io.*;
import java.util.*;
class P2 {
public static void main(String [] args) throws FileNotFoundException
{
Scanner data = new Scanner(new File("Exam.txt"));
Exam[] readObjects = readAllExams(data);
Exam[] collateObjects = collateExams(readObjects);
System.out.println("1) Initially the list of exams of students is: ");
System.out.println();
for(int i = 0; i < readObjects.length; i++)
{
System.out.println(readObjects[i]);
}
System.out.println();
System.out.println("Sorted list: ");
for (int i = 0; i < collateObjects.length; i++)
{
System.out.println(collateObjects[i]);
}
}
public static Exam[] readAllExams(Scanner s) throws ArrayIndexOutOfBoundsException
{
String firstName = "";
String lastName = "";
int ID = 0;
String examType = "";
char examTypeCasted;
int score = 0;
int index = 0;
Exam[] object = new Exam[s.nextInt()];
while(s.hasNext())
{
//Returns firtsName and lastName
firstName = s.next();
lastName = s.next();
//Returns ID number
if(s.hasNextInt())
{
ID = s.nextInt();
}
else
s.next();
//Returns examType which is 'M' or 'F'
examType = s.next();
examTypeCasted = examType.charAt(0);
if(s.hasNextInt())
{
score = s.nextInt();
}
//Exam[] object = new Exam[s.nextInt()];
object[index] = new Exam(firstName, lastName, ID, examTypeCasted, score);
//System.out.println();
index++;
}
readExam(s);
return object;
}
public static Exam readExam(Scanner s)
{
String firstName = "";
String lastName = "";
int ID = 0;
String examType = "";
char examTypeCasted = 0;
int score = 0;
while (s.hasNext())
{
//Returns firtsName and lastName
firstName = s.next();
lastName = s.next();
//Returns ID number
if(s.hasNextInt())
{
ID = s.nextInt();
}
//Returns examType which is 'M' or 'F'
examType = s.next();
examTypeCasted = examType.charAt(0);
if(s.hasNextInt())
{
score = s.nextInt();
}
}
Exam temp = new Exam(firstName, lastName, ID, examTypeCasted, score);
return temp;
}
public static Exam[] collateExams(Exam[] exams)
{
Exam[] r = new Exam[exams.length];
int [] position = new int[exams.length];
int index = 0;
for (int i = 0; (i < exams.length) && (i < position.length); i++)
{
position[i] = -1;
if(exams[i].getExamType()=='m')
{
if(position[exams[i].getID()]==-1)
{
r[index*2] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
position[exams[i].getID()] = 2*index;
}
else
r[2*position[exams[i].getID()] - 1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
}
else
{
if(position[exams[i].getID()]==-1)
{
r[index*2+1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
position[exams[i].getID()] = 2*index+1;
}
else
r[2*position[exams[i].getID()]+1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
}
index++;
}
return r;
}
}
答案 0 :(得分:0)
这是绝对完整的kluge,但它符合您的标准。如果这对您不起作用,那么您必须查看对象创建或其他内容以满足您的需求。
static int maxId = 0;
public static void main(String [] args) throws FileNotFoundException
{
Scanner data = new Scanner(Answer28522397.class.getClassLoader().getResourceAsStream("exams.txt"));
Exam[] readObjects = readAllExams(data);
System.out.println("1) Initially the list of exams of students is: ");
System.out.println();
for(int i = 0; i < readObjects.length; i++)
{
System.out.println(readObjects[i]);
}
Object[][] collatedObjects = collateExams(readObjects);
System.out.println("");
System.out.println("Sorted list: ");
for (int i = 0; i < collatedObjects.length; i++)
{
if (collatedObjects[i][0] != null && collatedObjects[i][1] != null)
{
System.out.println(collatedObjects[i][0]);
System.out.println(collatedObjects[i][1]);
}
}
}
public static Exam[] readAllExams(Scanner s) throws ArrayIndexOutOfBoundsException
{
int index = 0;
Exam[] object = new Exam[s.nextInt()];
while(s.hasNext())
{
Exam e = readExam(s);
maxId = e.getID() > maxId ? e.getID() : maxId;
object[index] = e;
index++;
}
return object;
}
public static Exam readExam(Scanner s)
{
String firstName = "";
String lastName = "";
int ID = 0;
String examType = "";
char examTypeCasted = 0;
int score = 0;
//Returns firtsName and lastName
firstName = s.next();
lastName = s.next();
//Returns ID number
if(s.hasNextInt())
{
ID = s.nextInt();
}
//Returns examType which is 'M' or 'F'
examType = s.next();
examTypeCasted = examType.charAt(0);
if(s.hasNextInt())
{
score = s.nextInt();
}
return new Exam(firstName, lastName, ID, examTypeCasted, score);
}
public static Object[][] collateExams(Exam[] exams)
{
Exam [] r = new Exam[exams.length];
System.arraycopy(exams, 0, r, 0, exams.length);
Object[][] collation = new Object[maxId+1][2];
for(Exam exam : exams)
{
if (exam.getExamType() == 'm')
{
collation[exam.getID()][0] = exam;
}
else
{
collation[exam.getID()][1] = exam;
}
}
return collation;
}