Reputation: 67
Originally I have the textfile which has lines:
12
Steve Jobs 9 final 91
Bill Gates 6 midterm 90
James Gosling 3 midterm 100
James Gosling 3 final 100
Dennis Ritchie 5 midterm 94
Steve Jobs 9 midterm 95
Dennis Ritchie 5 final 100
Jeff Dean 7 midterm 100
Bill Gates 6 final 96
Jeff Dean 7 final 100
Sergey Brin 27 final 97
Sergey Brin 22 midterm 99
Method public static Exam[] readAllExams(Scanner s) creates and returns an array of objects. Note that the first letters of strings 'final' and 'midterm' are converted into char. So, when I run readAllExams(), the result is:
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
But when I run public static Exam[] collateExams(Exam[] exams) it gives me a NullPointerException in this line: if(exams[i].getExamType()=='m')
. Can smb explain/help me with that? Note that getExamType() return a char.
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("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];
r = exams;
int [] position = new int[exams.length];
for(int i = 0;i < position.length;i++)
position[i]=-1;
int index = 0;
for(int i = 0; i < exams.length; i++)
{
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;
/*Exam [] r = new Exam[100];
r = exams;
int[] position = new int[100];
int index = 0;
for(int i = 0; i < exams.length; i++)
{
if(r[i].getExamType() == 'm')
{
r[index] = new Exam(r[index].getFirstName(), r[index].getLastName(),
r[index].getID(), r[index].getExamType(), r[index].getScore());
position[index] = r[index].getID();
index+=2;
/*if((r[index+1].getExamType() == 'F') && (position[index+1] == r[i].getID()))
{
r[index+1] = new Exam(r[index].getFirstName(), r[index].getLastName(),
r[index].getID(), r[index].getExamType(), r[index].getScore());
}
/*if(position[i].getID() == r[i].getID())
r[i] = new Exam(r[i].getFirstName(), r[i].getLastName(),
r[i].getID(), r[i].getExamType(), r[i].getScore());
}
else if((r[index+1].getExamType() == 'f') && position[index] == r[i].getID())
{
r[index + 1] = new Exam(r[index].getFirstName(), r[index].getLastName(),
r[index].getID(), r[index].getExamType(), r[index].getScore());
}
}
return r;*/
}
}
Upvotes: 1
Views: 127
Reputation: 539
When I ran your code, I wasn't getting a null pointer exception, but an index out of bounds exception when you were doing r*2. This won't work since you are allocating 12 spots for your array, iterating 12 items, and then telling it to go to n*2 on the iteration. It goes past the bounds quickly. There were also some other issues setting the ID as the array index, etc. I cleaned up your code for you some and followed what appeared to be your intent (allocate two spots, assuming that the number of exams would include two per ID, one M and one F). Note that your text file also includes an error in the ID for Sergey Brin which breaks the assumption. This probably isn't your final product, but it gets what you had there working:
public static void main(String [] args) throws FileNotFoundException
{
Scanner data = new Scanner(new File("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]);
}
Exam[] collateObjects = collateExams(readObjects);
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
{
int index = 0;
Exam[] object = new Exam[s.nextInt()];
while(s.hasNext())
{
object[index] = readExam(s);
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 Exam[] collateExams(Exam[] exams)
{
Exam [] r = new Exam[exams.length];
System.arraycopy(exams, 0, r, 0, exams.length);
int [] position = new int[exams.length];
for(int i = 0;i < position.length;i++)
position[i]=-1;
for(int i = 0; i < exams.length; i++)
{
int nextPos = determineNextPosition(position, exams[i]);
r[nextPos] = new Exam(exams[i].getFirstName(), exams[i].getLastName(), exams[i].getID(), exams[i].getExamType(), exams[i].getScore());
}
return r;
}
private static int determineNextPosition(int[] posArray, Exam exam)
{
int position = -1;
int id = exam.getID();
// see if there is a spot for the ID already
for (int i = 0; i < posArray.length; i++)
{
if (posArray[i] == id)
{
position = i;
break;
}
}
if (position < 0)
{
// if a spot wasnt found, make one
for (int i = 0; i < posArray.length; i+=2)
{
if (posArray[i] == -1)
{
posArray[i] = id;
posArray[i+1] = id;
position = i;
break;
}
}
}
if (position < 0) throw new RuntimeException("Something went really wrong");
if (exam.getExamType() == 'm')
{
return position;
}
else
{
return position + 1;
}
}
Upvotes: 1