これは私のコードです。1つのエラーを除いて、ほぼすべての方法で実行しています。
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
コンパイルしようとすると、このエラーが発生します。Eclipseを使用しています。
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
at Proj5.gradeSingleQuiz(Proj5.java:55)
at Proj5.main(Proj5.java:27)
問題の行それ
userAnswersInt[p][j] = ara[j]/10;
私はそれを自分で理解できないようです、私がここで見ない何かがありますか、それとも私はこれを完全に間違った方法で行っていますか?
前もって感謝します
編集:
txtファイルからのtxtは次のとおりです。
4563123,112211324135412
2312311,222121324135211
2312345,112211324135421
5527687,212111313124412
7867567,111111111111111