I am attempting to write a code (yes its homework) that reads student test responses and cross checks them with the answer key in java. I am getting an error code and would appreciate understanding the problem.
First here is my code.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
/**
*
* @author lvinikoor
*/
public class studentGrades {
public static void main(String[] args) {
// Student Data
boolean[][] studentAnswers = new boolean[2000][1];
String responses = "//Users//lvinikoor//Desktop//StudentResponses.txt";
// Answers data
boolean answers[] = new boolean[200];
String answersFile = "//Users//lvinikoor//Desktop//TestAnswers.txt";
int[] allCorrect = new int[100]; // stores indexes of students who scored all correct
int[] score = new int[100]; // scores of students
try {
BufferedReader br = new BufferedReader(new FileReader(responses));
int row = 0;
String temp = "/n";
while ((temp = br.readLine()) != null)
{
if (row >= 2000)
{
break; // break loop if file contains more than 2000 student entries
}
String[] tokens = temp.split("/n "); // Assumming each line in txt
// file is one student and
// results are separated by
// space for each question.
for (int i = 0; (i < tokens.length && i < 20); i++)
{
studentAnswers[row][i] = Boolean.valueOf(tokens[i]);
}
row++;
}
}
catch (IOException e)
{
System.out.println("ERROR : " + e);
}
// Reading answers from file
try {
BufferedReader br = new BufferedReader(new FileReader(answersFile));
int index = 0;
String temp = "/n";
while ((temp = br.readLine()) != null) {
answers[index] = Boolean.valueOf(temp); // Assuming each line in
// answers.txt file is
// answer for each
// question
index++;
}
} catch (IOException e) {
System.out.println("ERROR: " + e);
}
// Prints correct answers of each student
for (int i = 0; i < 100; i++) {
System.out.print("Student " + (i + 1) + " -> ");
int noOfCorrect = 0;
for (int j = 0; j < 20; j++) {
if (studentAnswers[i][j] == answers[j]) {
System.out.print(j + "\t");
noOfCorrect++;
} else {
System.out.print("-" + "\t");
}
}
if (noOfCorrect == 20) {
allCorrect[i] = i;
}
score[i] = noOfCorrect * 5;
System.out.println("\nNo of correct answers : " + noOfCorrect);
System.out.println("Grade Score : " + getGrade(score[i]));
}
// Average Grade Score and Standard Deviation
HashMap<String, List<Integer>> map = new HashMap<>();
map.put("A", new ArrayList<Integer>());
map.put("B", new ArrayList<Integer>());
map.put("C", new ArrayList<Integer>());
map.put("D", new ArrayList<Integer>());
map.put("F", new ArrayList<Integer>());
for (int studentScore : score) {
String grade = getGrade(studentScore);
switch (grade) {
case "A":
map.get("A").add(studentScore);
break;
case "B":
map.get("B").add(studentScore);
break;
case "C":
map.get("C").add(studentScore);
break;
case "D":
map.get("D").add(studentScore);
break;
case "F":
map.get("F").add(studentScore);
break;
}
}
I am getting the following error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at studentGrades.main(studentGrades.java:72) Student 1 -> 0 /Users/lvinikoor/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)
I have narrowed it down to this particular section of code.
// Prints correct answers of each student
for (int i = 0; i < 100; i++) {
System.out.print("Student " + (i + 1) + " -> ");
int noOfCorrect = 0;
for (int j = 0; j < 20; j++) {
if (studentAnswers[i][j] == answers[j]) {
System.out.print(j + "\t");
noOfCorrect++;
} else {
System.out.print("-" + "\t");
}
The goal was that If (student answers of student I was J and answers was J than it would count as a correct answer.
Can someone point me in the right direction as to why this is causing an issue with the Array Index being out of Bounds?