CODE
import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;
public class Grader
{
public static void main (String[] args) throws IOException
{
System.out.println ("Exam Scoring...");
System.out.println();
String key, line, SA; //Answer key, Student ID and Student answers
Scanner fileScan, lineScan, SAScan; //Scans text file and scans ID and response
double score = 0, avg; //numerical grade and average
double avgscore = 0; //running score
int lineCount = 0; //counts number of lines
DecimalFormat fmt = new DecimalFormat ("0.00");
fileScan = new Scanner (new File ("C:\\Users\\Ibby\\Project2\\src\\Students.txt"));
key = fileScan.next(); //Scans answer key
while (fileScan.hasNext())
{
line = fileScan.next();
lineScan = new Scanner (line);
lineScan.useDelimiter("/");
System.out.print ("" + lineScan.next());
SA = lineScan.next(); //Scans students response
SAScan = new Scanner (SA);
lineCount++;
avgscore += score;
score = score - score;
for (int i = 0; i < SA.length(); i++)
{
char response = SA.charAt(i);
char correct = key.charAt(i);
if (response == correct)
score+=5;
if (response != correct)
score-=2;
}
System.out.print (" " + fmt.format(score) + " ");
if (score <= 100.00 && score >=93.00)
System.out.println ("A");
else if (score <=92.99 && score >=90.00)
System.out.println ("A-");
else if (score <= 89.99 && score >=87.00)
System.out.println ("B+");
else if (score <=86.99 && score >=83.00)
System.out.println ("B");
else if (score <=82.99 && score >= 80.00)
System.out.println ("B-");
else if (score <=79.99 && score >=77.00)
System.out.println ("C+");
else if (score <=76.99 && score >=73.00)
System.out.println ("C");
else if (score <=72.99 && score >=70.00)
System.out.println ("C-");
else if (score <=69.99 && score >=67.00)
System.out.println ("D+");
else if (score <=66.99 && score >=63.00)
System.out.println ("D");
else if (score <=62.99 && score >=60.00)
System.out.println ("D-");
else if (score <=59.99)
System.out.println ("F");
}
System.out.println();
avg = (avgscore + score)/lineCount;
System.out.println ("Average Score: " + fmt.format(avg));
}
}
edit: code tags PB
Reads from a text file which has answer key as first line
Next line has student id followed by the student answers
print id followed by numerical grade and letter grade
correct answer earns 5 points, wrong answer 2 point minus, no answer (blank) earns 0 points
equate average
I have everything to work except when there is a blank space in the text file where the student answer is. I get an error. This is because white space automatically becomes a delimiter in the scanner class. How can I make it so that the whitespace is read as a character??
This post has been edited by PennyBoki: 10 Oct, 2007 - 04:43 PM