In Java Only: My program is not working correctly, The history teacher at your school needs help grading a true/false test.The student's IDs and test answers are stored ina file, The first entry in the file contains the answers to the test in the following form: TFFTFFTTTTFFTFTFTFTT Every other entry in the file is the student;s ID, followed by a blank, followed by the student;s response. For example, the entry: ABC54301 TFTFTFTT TFTFTFFTTFT indicstes that the student;s ID is ABC54301 and the answer to question 1 is True, the answer to question 2 is false, and so on. This student did not answer question 9. he exam has 20 questions, and the class has more than 150 students. Each correct answer is awarded 2 points, each wrong answer gets -1 point, and no answer gets 0 points. Write a program that processes the test data. The output should be the student's ID, followed by the student's answers, followed by the test score, and then followed by the test grade. Here is my code, I keep getting a NullPointerException when I am calling the method cacluatePoints and when checking the students answers is = to ' ' in the calculatePoints method. I tried everything. Thank you My Code: public class EditededHistoryTest { static final int MAX_QUESTIONS = 20; static final int MAX_NUM_OF_STUDENTS = 12; /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // String[] answerKey = new String[MAX_QUESTIONS]; String[] studentID = new String[8]; //String studentID; // String[] studentAnswers = new String[MAX_QUESTIONS]; char[] answerKey = new char[MAX_QUESTIONS]; char[] studentAnswers = new char [MAX_QUESTIONS]; int score;//send this score into the method CalculatePercentgegrade int[]percentageScore = new int[MAX_QUESTIONS]; char[]testGrade = new char[MAX_NUM_OF_STUDENTS]; //int len; int i;//initialize the index to start at counting at zero int[]studentScores = new int[MAX_NUM_OF_STUDENTS]; String inputLine; int blankSpace; Scanner inFile = new Scanner(new FileReader("e:\\historytestmodule.txt")); PrintWriter outFile = new PrintWriter("historytestmoduleOut.dat"); outFile.println("GRADING DATA: "); System.out.println("GRADING DATA: "); //Read the answer key inputLine = inFile.nextLine(); //inputLine = inFile.nextLine(); // answerKey[i] = inFile.nextLine(); for(i = 0; i < answerKey.length; i++){ // answerKey[i]= inputLine.substring(0,inputLine.length()); answerKey[i]= inputLine.charAt(i); // if(answerKey[i] = inputLine.charAt(i) == null) // {System.out.println("kkkanswerkey array is null"); // } //else{ // System.out.println("KKKKanswerkey array is not null"); System.out.print(answerKey[i]); }System.out.println("the length of the input line " + inputLine.length()); System.out.println("Whats in the iinput line after you gettheanswer key" + inputLine); // System.out.println(" Answer Key:" + " " + answerKey[i]); // outFile.printf("Answers Key: %20s%n",answerKey[i]); int index = 0; //get the student's IDs and the student's answers int numOfStudents = 0; while(inFile.hasNext() && numOfStudents < MAX_NUM_OF_STUDENTS) { //Get the first students' ID and test answers inputLine = inFile.next(); studentID[index] = inputLine.substring(0) //Go to the next line and get the student's test answers and //get the index of blankspace between the text inputLine = inFile.nextLine(); blankSpace = inputLine.indexOf(' ');//at this point the kength o for(i = 0; i < studentAnswers.length; i++){ studentAnswers[i] = inputLine.substring(blankSpace + 1, inputLine.length()); } System.out.println(); numOfStudents++; } System.out.println(calculatePoints(answerKey, studentAnswers // for(i = 0; i < studentAnswers.length; i++) //{ //} //calculatePercentageScore(studentScores, maxPoints);//might have to send this from the method // CalculateGrades(studentScores, testGrade); //Do output here //System.out.println("the kids grades" + testGrade ); //System.out.println(studentID + studentAnswers + calculatePoints(answerKey, studentAnswers )); //System.out.printf(String.format("%s %s %2d %c %n",studentID, studentAnswers,calculatePoints(answerKey, studentAnswers))); // outFile.printf(String.format("%9s %21s %n", // studentID[i],studentAnswers[i]));///the student grades have to print here // outFile.printf(String.format("%9s %21s %2d %2c%n", // studentID[i],studentAnswer, score, testGrade(score))); //inFile.close(); outFile.close(); }//end of the Main method public static int calculatePoints(char[]keyAns, char[]stuAnswers) { //int len = keyAns.length; int testScores = 0; int i; int correct = 0,incorrect = 0, omit = 0 // sum = 0; for(i = 0; i < stuAnswers.length; i++) { if(stuAnswers[i] == ' ')//this has a null pointer exception { omit = 0; } else if(stuAnswers[i]== keyAns[i]) { correct = correct + 2; } else { incorrect = incorrect - 1; } testScores = correct - incorrect + omit; return testScores; } }//end of HistoryTest Class
In Java Only: My program is not working correctly, The history teacher at your school needs help grading a true/false test.The student's IDs and test answers are stored ina file, The first entry in the file contains the answers to the test in the following form: TFFTFFTTTTFFTFTFTFTT
Every other entry in the file is the student;s ID, followed by a blank, followed by the student;s response. For example, the entry: ABC54301 TFTFTFTT TFTFTFFTTFT
indicstes that the student;s ID is ABC54301 and the answer to question 1 is True, the answer to question 2 is false, and so on. This student did not answer question 9. he exam has 20 questions, and the class has more than 150 students.
Each correct answer is awarded 2 points, each wrong answer gets -1 point, and no answer gets 0 points. Write a program that processes the test data. The output should be the student's ID, followed by the student's answers, followed by the test score, and then followed by the test grade.
Here is my code, I keep getting a NullPointerException when I am calling the method cacluatePoints and when checking the students answers is = to ' ' in the calculatePoints method. I tried everything. Thank you
My Code:
public class EditededHistoryTest {
static final int MAX_QUESTIONS = 20;
static final int MAX_NUM_OF_STUDENTS = 12;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException
{
// String[] answerKey = new String[MAX_QUESTIONS];
String[] studentID = new String[8];
//String studentID;
// String[] studentAnswers = new String[MAX_QUESTIONS];
char[] answerKey = new char[MAX_QUESTIONS];
char[] studentAnswers = new char [MAX_QUESTIONS];
int score;//send this score into the method CalculatePercentgegrade
int[]percentageScore = new int[MAX_QUESTIONS];
char[]testGrade = new char[MAX_NUM_OF_STUDENTS];
//int len;
int i;//initialize the index to start at counting at zero
int[]studentScores = new int[MAX_NUM_OF_STUDENTS];
String inputLine;
int blankSpace;
Scanner inFile = new Scanner(new FileReader("e:\\historytestmodule.txt"));
PrintWriter outFile = new PrintWriter("historytestmoduleOut.dat");
outFile.println("GRADING DATA: ");
System.out.println("GRADING DATA: ");
//Read the answer key
inputLine = inFile.nextLine();
//inputLine = inFile.nextLine();
// answerKey[i] = inFile.nextLine();
for(i = 0; i < answerKey.length; i++){
// answerKey[i]= inputLine.substring(0,inputLine.length());
answerKey[i]= inputLine.charAt(i);
// if(answerKey[i] = inputLine.charAt(i) == null)
// {System.out.println("kkkanswerkey array is null");
// }
//else{
// System.out.println("KKKKanswerkey array is not null");
System.out.print(answerKey[i]);
}System.out.println("the length of the input line " + inputLine.length());
System.out.println("Whats in the iinput line after you gettheanswer key" + inputLine);
// System.out.println(" Answer Key:" + " " + answerKey[i]);
// outFile.printf("Answers Key: %20s%n",answerKey[i]);
int index = 0;
//get the student's IDs and the student's answers
int numOfStudents = 0;
while(inFile.hasNext() && numOfStudents < MAX_NUM_OF_STUDENTS)
{
//Get the first students' ID and test answers
inputLine = inFile.next();
studentID[index] = inputLine.substring(0)
//Go to the next line and get the student's test answers and
//get the index of blankspace between the text
inputLine = inFile.nextLine();
blankSpace = inputLine.indexOf(' ');//at this point the kength o
for(i = 0; i < studentAnswers.length; i++){
studentAnswers[i] = inputLine.substring(blankSpace + 1, inputLine.length());
} System.out.println();
numOfStudents++;
}
System.out.println(calculatePoints(answerKey, studentAnswers
// for(i = 0; i < studentAnswers.length; i++)
//{
//}
//calculatePercentageScore(studentScores, maxPoints);//might have to send this from the method
// CalculateGrades(studentScores, testGrade);
//Do output here
//System.out.println("the kids grades" + testGrade );
//System.out.println(studentID + studentAnswers + calculatePoints(answerKey, studentAnswers ));
//System.out.printf(String.format("%s %s %2d %c %n",studentID, studentAnswers,calculatePoints(answerKey, studentAnswers)));
// outFile.printf(String.format("%9s %21s %n",
// studentID[i],studentAnswers[i]));///the student grades have to print here
// outFile.printf(String.format("%9s %21s %2d %2c%n",
// studentID[i],studentAnswer, score, testGrade(score)));
//inFile.close();
outFile.close();
}//end of the Main method
public static int calculatePoints(char[]keyAns, char[]stuAnswers)
{
//int len = keyAns.length;
int testScores = 0;
int i;
int correct = 0,incorrect = 0, omit = 0
// sum = 0;
for(i = 0; i < stuAnswers.length; i++)
{
if(stuAnswers[i] == ' ')//this has a null pointer exception
{ omit = 0;
}
else if(stuAnswers[i]== keyAns[i])
{
correct = correct + 2;
}
else {
incorrect = incorrect - 1;
}
testScores = correct - incorrect + omit;
return testScores;
}
}//end of HistoryTest Class
Step by step
Solved in 2 steps with 2 images