Hi all - First time poster. I've run into a little trouble with my Java assignment. The idea of the program is to grade exams.
The program takes a text file that contains:
- First line is the correct answers
- Following lines contain student ID's separated by a " " and their answers to the exam.
It grades each exam, calculates the score to the .00 place and the letter grade.
The results should be written to an output file of the same name as the input, only with a '.out' extension in the format:
"StudentID Grade LetterGRADE"
My Problems:
- I get a "Exception in thread "main" java.lang.NullPointerException at Grader.main(Grader.java:36)" upon completion of the program.
- The Program works (as far as I can tell by the commented System.out.println on line 52) but it DOES NOT actually write anything to the answers.out file (it creates the file but it is empty)
Here is the 'answers.txt' input file:
And my code so far:DACABDBDABAEDEDBBEDEBCCBABCDDB
885265785 DACABDBDABAEDEDBBEDEBCCCABCDDB
576034603 DACABDBDABAEDEDBBEDEBCCBABCDDB
702860517 EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
765841670 DACABDBDABAEDEDBAEDEBCCBABCDDB
024022503 DACABDBDABAEDEDBBEDEBCCBABCDDB
204735425 ACABDBDABAEDEDBBEDEBCCBABCDDBD
203521084 DACABDBDABAEDEDBBEDEBCCBABCDDB
802426407 DACABDBDABAEDEDBBEDEBCCBABCDDB
432176337 CCDCCDCCCCACCCDCCCCCCCCBCCCCCC
180867781 DACABDBDABAEDEDBBEDEBCCBABCDDB
027781302 DACABCBDABAEDEDBBEDDBCCBABCDDB
438787273 DACABDBDABAEDEDBBEDEBCCBABCDDB
831352188 DACABDBDABAEDEDBBEDEBCCBABCDDB
360447237 DACABDBDABAEDEDBBEDEBCCBABCDDB
316480278 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
151351806 DACABDBDABAEDEDBBEDEBCCBABCDDB
447056164 DACABDBDABAEDEDBBEDEBCCBABCDDB
527577044 DACABDBDABAEDEDBBEDEBCCBABCDDB
072704112 DACABDBDCBAEDEDBEEDEBCCBBBCDDB
353334086 DACABDBDABAEDEDBBEDEBCCBABCDDB
642777412 DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
040217240 DACABDBDABAEDEDBBEDEBCCBABCDDB
843525013 DACABBBDAAAEDEDBBEDCCCCBABCDDB
183524468 DACABDBDABAEDEDBBEDEBCCBABCDDB
042176072 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
108523756 DACABDBDABAEDEDBBEDEBCCBABCDDB
567838552 DACABDBDABAEDEDBBEDEBCCBABCDDB
360447238 DADABDBDAAAEDEDCBEDEBCCBDBCDDB
360447240 AACABDCDABAEDEDBBADEBCCCABCDDB
import java.io.*; import javax.swing.JOptionPane; public class Grader { public static void main ( String[] args ) { String line, file; char[] key, answers; char letter; String[] fields; String ans, id; int i = 0, count = 0; double grade, correct=0; file = JOptionPane.showInputDialog(null, "Please enter the name of your '.txt' answers file in the correct format"); try { BufferedReader br = new BufferedReader (new FileReader(file + ".txt")); PrintWriter pw = new PrintWriter( new FileWriter(file +".out")); line = br.readLine(); key = line.toCharArray(); while (line != null) { line = br.readLine(); fields = line.split(" "); id = fields[0]; ans = fields[1]; answers = ans.toCharArray(); while (count < answers.length) { if (answers[i] == key[i]) { correct++; } count++; i++; } grade = 100*(correct/30); letter = (char)letterGrade(grade); //System.out.println(id + " " + String.format( "%1$.2f", grade) + " " + letter + "\n"); pw.println(id + " " + String.format( "%1$.2f", grade) + " " + letter + "\n"); count = 0; correct = 0; i = 0; } br.close(); pw.close(); } catch( IOException e) { System.out.println("File ERROR"); } /* try { FileWriter fw = new FileWriter ("answers.out"); PrintWriter pw = new PrintWriter (fw); } catch( IOException e) { System.out.println("ERROR"); }*/ } public static char letterGrade (double grade) { char a = 'A', b = 'B', c = 'C', d = 'D', f = 'F'; if (grade >= 90) {return a;} else if (grade >= 80 && grade < 90) {return b;} else if (grade >= 70 && grade < 80) {return c;} else if (grade >= 60 && grade < 70) {return d;} else {return f;} } }
Any help appreciated, Thank you!