简体   繁体   中英

giving me an out of bounds error with an array and im not sure how to fix it

This is my code, i have it pretty much all the way done except for one error it seems:

import java.util.*;
import java.io.*;

public class Proj5 {
public static void main(String[] args)throws IOException{
    Scanner s = new Scanner(System.in);
    int [] quizKey = {1,1,2,2,1,1,3,2,4,1,3,5,4,1,2};
    String [] userAnswers = new String[100];
    String [] wid = new String[100];
    int [][] userIndividualAnswers = new int[quizKey.length][userAnswers.length];
    int [] numCorrect = new int[quizKey.length];
    int max;
    int min;

    int lines=0;
    readInText();
    s = readInText();
    while(s.hasNext()){
        String line = s.nextLine();
        String[] tokens = line.split(",");
        wid[lines] = tokens[0];
        userAnswers[lines] = tokens[1];
        lines ++;

    }// end while loop
    Long[][] userAnswersInt = new Long[quizKey.length][lines];
    numCorrect = gradeSingleQuiz(lines, quizKey, userAnswers, numCorrect, userAnswersInt);
    double[] percentCorrect = new double[lines];
    percentCorrect = percentCorrect(lines, numCorrect, quizKey);
    char[] grades = new char[lines];
    grades = grade(numCorrect, lines);

    displayOutput(wid, lines, numCorrect, grades, percentCorrect);
}//end main

public static Scanner readInText()throws IOException{
    Scanner inFile = new Scanner(new File("QuizScores.txt"));
    return inFile;


}// end readInText

public static String[] userAnswers(String userAnswers[]){
    return userAnswers;
}

public static int[] gradeSingleQuiz(int lines, int quizKey[], String userAnswers[], int numCorrect[], Long userAnswersInt[][]){
    for (int j=0; j<lines; j++){
    numCorrect[j]=0;
        long[] ara = new long[userAnswers.length];
        long[] abc = new long[userAnswers.length];
        ara [j] = Long.parseLong(userAnswers[j]);
    for(int p=0; p<ara.length; p++){
        abc [p] = ara[j]%10;
        userAnswersInt[p][j] = ara[j]/10;
    }
    for(int n=0; n<=quizKey.length; n++){

        if(userAnswersInt[j][n]==(quizKey[n])){
                    numCorrect[j]++;    
            }
    }
    }//end for loop

return numCorrect;
}// end gradeSingleQuiz

public static int max(int max, int numCorrect[]){
    max = numCorrect[0];
    for(int r=1; r<numCorrect.length; r++){
        if(numCorrect[r]>max){
            max=numCorrect[r];
        }
    }
return max;
}

public static int min(int min, int numCorrect[]){
    min = numCorrect[0];
    for(int r=1; r<numCorrect.length; r++){
        if(numCorrect[r]<min){
            min=numCorrect[r];
        }
    }
return min;
}

public static char[] grade(int numCorrect[], int lines){
    char[] grade = new char[lines];
    for (int j=0; j<=lines; j++){

        if(numCorrect[j]>=14)
            grade[j]='A';
        else if((numCorrect[j]>=12)&&(numCorrect[j]<14))
            grade[j]='B';
        else if((numCorrect[j]>=11)&&(numCorrect[j]<12))
            grade[j]='C';
        else if ((numCorrect[j]>=9)&&(numCorrect[j]<11))
            grade[j]='D';
        else
            grade[j]='F';
    }
    return grade;
}//end grade


public static double[] percentCorrect(int lines, int numCorrect[], int quizKey[]){
    double[] centCorrect = new double[100];
    for (int j=0; j<=lines; j++){
    centCorrect[j] = numCorrect[j]/quizKey.length;
    }
return centCorrect;
}

public static void averageScore(int lines, double percentCorrect[]){
    double add=0;
    for(int d=0; d<=lines; d++){    
        add = percentCorrect[d] + add;
    }//end for loop
    System.out.println("Average: " + add + "%");
}// end averageScore

public static void displayOutput(String wid[], int lines, int numCorrect[], char grades[], double percentCorrect[]){
    System.out.println("Student ID    # Correct    %Correct    Grade");
    for(int i=0; i<lines; i++){
        System.out.println(wid[0] + "    " + numCorrect[i] + "    " +
                (percentCorrect[i]) + "    " + grades[i]);  
    }

}// end display output

}//end class

It give me this error when i try and compile it, i am using eclipse:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
at Proj5.gradeSingleQuiz(Proj5.java:55)
at Proj5.main(Proj5.java:27)

The line in question it

userAnswersInt[p][j] = ara[j]/10;

I can't seem to figure it out myself, is there something i'm not seeing here or am i just completely going about this the wrong way?

Thanks in advance

EDIT:

txt from the txt file is:

4563123,112211324135412
2312311,222121324135211
2312345,112211324135421
5527687,212111313124412
7867567,111111111111111

In gradeSingleQuiz() method, the array ara is initialized with the size of userAnswersArray , but you loop on it from j=0 to j=lines-1. The array userAnswersArray is always of size 100, but lines 's length depends on the input from the console.

I checked your logic and found the following logical errors

  1. You assigned the array ara to length 100, and then tried to loop on an array object which is only 15 elements long

    long[] ara = new long[userAnswers.length];

  2. the line below is returning null and is thus throwing an null pointer exception.

    if(userAnswersInt[j][n]==(quizKey[n]))

Ill suggest you revisit the logic, post the requirement here if help needed, the logic can be helped with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM