0

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?

  • If you want to stay with this style of coding take a step debugger. Otherwise remove all these numeric literals and replace them with well named constants. The error will be obvious then. – blafasel Sep 30 '17 at 20:47
  • 2
    ArrayIndexOutOfBounds means that you are trying to access an index which is higher than what is the defined one. In this case, you declare `studentAnswers` with size of 2000 by 1. But then, you are trying to access answers with `studentAnswers[i][j]`. The `j` in first iteration will be 0, in second will be 1, but in third, it will be 2, which is above what you defined (i.e `[2000][1]`) – Nothing Nothing Sep 30 '17 at 20:49
  • @blafasel I did the debugger and thats how I got to the conclusion that I did, but good advice on changing the style of the code to better understand, I will try to do that moving forward. – Lainie Grace Sep 30 '17 at 20:54
  • @NothingNothing Thank you! I guess I'm confused because when I first defined it I thought i was defining the array as being 2000 lines with 1 answer in each line. But thanks for the clarification! – Lainie Grace Sep 30 '17 at 20:57

0 Answers0