Welcome to Dream.In.Code
Become a Java Expert!

Join 149,585 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,777 people online right now. Registration is fast and FREE... Join Now!




Exam scoring program

 
Reply to this topicStart new topic

Exam scoring program, reads answers from text file

ibbyisdaman
10 Oct, 2007 - 04:39 PM
Post #1

New D.I.C Head
*

Joined: 10 Oct, 2007
Posts: 2


My Contributions
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
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Exam Scoring Program
10 Oct, 2007 - 04:47 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
if that is the problem use the StringBuffer class and one of it's methods to remove the blanks and then just assign that to a new string, and work with it.

And welcome to </dream.in.code>, and please use code tags when posting code, thx smile.gif
User is offlineProfile CardPM
+Quote Post

ibbyisdaman
RE: Exam Scoring Program
10 Oct, 2007 - 05:02 PM
Post #3

New D.I.C Head
*

Joined: 10 Oct, 2007
Posts: 2


My Contributions
sorry for not using code tags. I am not familiar with the StringBuffer class, but again the String in the text file after the delimiter "\" is Scanned. The whole String is not being scanned because there are blanks, which means the student did not answer one of the multiple choice questions. The text file looks like this:

ABCDABCDABCDABCD
123456789/ABBDABCD BCDAB D
987654321/BBC AAC ABCDA CB

User is offlineProfile CardPM
+Quote Post

sl4ck3r
RE: Exam Scoring Program
10 Oct, 2007 - 08:48 PM
Post #4

D.I.C Head
Group Icon

Joined: 22 Sep, 2007
Posts: 119


Dream Kudos: 25
My Contributions
i think its not seeing the / as a / try putting // and see it works it thinks your trying to escape the " i believe.

This post has been edited by sl4ck3r: 10 Oct, 2007 - 08:53 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:56PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month