具有文件读取功能的阵列列表程序

时间:2018-11-30 16:56:46

标签: java file arraylist compiler-errors

我已经创建了2个类,一个是执行代码的驱动程序类,并且从该类调用所有方法。该代码需要从我的文件中读取信息,并将数据存储在StudentScore的ArrayList中(我的其他类)。运行代码时,出现此错误

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
    at java.util.ArrayList.rangeCheck(ArrayList.java:657)
    at java.util.ArrayList.get(ArrayList.java:433)
    at StudentScore.toString(StudentScore.java:153)
    at ScoreDriver.main(ScoreDriver.java:37)

这是我的驱动程序类

import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
public class ScoreDriver extends StudentScore{
public static void main(String[] args) throws Exception {

    String fileName = "scores.txt";
     String line = null;
        ArrayList<Integer>studentIDNumber = new ArrayList<Integer>();//Set student ID as an arraylist
        ArrayList<Integer>verbalScore = new ArrayList<Integer>();//Set verbal score as an arraylist
        ArrayList<Integer>mathScore = new ArrayList<Integer>();// Set math score as an arraylist


    FileReader fileReader = new FileReader(fileName); //Read from file 


    BufferedReader bufferedReader = new BufferedReader(fileReader);


    while((line = bufferedReader.readLine()) != null) {
        String[] values = line.split(",",-1);    
        if(values[0].length() > 1 )
        {
            System.out.println(line);
            studentIDNumber.add(Integer.parseInt(values[0])); // the first value is the ID number
            verbalScore.add(Integer.parseInt(values[1]));// Second value is verbal score
            mathScore.add(Integer.parseInt(values[2])); // third value is math score
        }   
    }   
    bufferedReader.close(); //Closing file
    StudentScore value = new StudentScore();// Instantiate what a student score is 
    value.setstudentIDNumber(studentIDNumber); // Includes a ID number, verbal score, math score and total score
    value.setverbalScore(verbalScore);
    value.setmathScore(mathScore);
    value.gettotalScore();  

    value.toString(15630);// Input any student ID number that you want to look up to check that students scores 

    System.out.println("The average Evidence-base Reading and Writing Score is: " + AverageVerbalScore());
    System.out.println("The average Math score is: " + AverageMathScore());
    System.out.println("The average combined score is: " + AverageCombinedScore());
    System.out.println("The total number of unique test-takers is: " + UniqueTest());

    value.HighestCombined(15630);// Input the student ID number to see what their best math score, verbal score and superscore is

    System.out.println("The highest Evidence-base Reading and Writing score is: " + HighestVerbal());
    System.out.println("The lowest Math score is: " + LowestMath());
}   
}

这是我的另一堂课

import java.util.ArrayList;

public class StudentScore 
{// Instantiate a Student's Score
private static ArrayList<Integer>studentIDNumber;
private static ArrayList<Integer>verbalScore;
private  static ArrayList<Integer>mathScore;
public static ArrayList<Integer>totalScore = new ArrayList<Integer>();

public void setstudentIDNumber(ArrayList<Integer> value)// Setting a 
studentIDNumber
{
    this.studentIDNumber = value;
}
public void setverbalScore(ArrayList<Integer> value)//Setting a verbal 
score
    {
    this.verbalScore = value;
    }
public void setmathScore(ArrayList<Integer> value)// Setting a math 
score
    {
    this.mathScore = value;
    }
public void gettotalScore()// calculate total score
    {
    for (int i = 0; i < verbalScore.size(); i++) 
    {     
        int value = ((verbalScore.get(i) + mathScore.get(i))); // total 
score is the verbal and math combined
        totalScore.add((value)); //add value to total score 

        System.out.println(totalScore.get(i));
    }
}
public static int AverageVerbalScore()// Calculate average verbal score 
from the whole file 
{   
    int sum = 0;
    for(int i = 0; i < verbalScore.size(); i++)
        sum += verbalScore.get(i); 

    return sum/verbalScore.size();

    }

public static int AverageMathScore()//Calculate average math score from 
the whole file
{
int sum = 0;
for(int i = 0; i < mathScore.size(); i++)
    sum += mathScore.get(i);

return sum/mathScore.size();

}
public static int AverageCombinedScore()//Calculate average total Score 
from the whole file
{
int sum = 0;
for(int i = 0; i < totalScore.size(); i++)
    sum += totalScore.get(i);

return sum/totalScore.size();

}

public void HighestCombined(int studentID)//Method to find highest 
score of an inputed student ID number 
{
int rowNumber[]=new int[100];
int i = 0;
int bestTotalScore = 0;

    if(studentIDNumber.contains(studentID))
    {
        rowNumber[i] = studentIDNumber.indexOf(studentID);

        for(int j = 0; j < 10; j++)
        {

            if(studentIDNumber.get(studentIDNumber.indexOf(studentID) + j + 1 ) == studentID)
            {

                i++;
                rowNumber[i] = studentIDNumber.indexOf(studentID) + j + 1;                  
            }   
        }
        for(i = 0; i < rowNumber.length; i++)   
        {
            if(rowNumber[i] >= 1 )  
            {
                if(totalScore.get(rowNumber[i]) > bestTotalScore)
                {
                    bestTotalScore = totalScore.get(rowNumber[i]);
                }                   
            }   
        }
        System.out.println("The highest superscore for student ID " + studentID + " is: " + bestTotalScore);
        }
    else
    {
        System.out.println("Student ID does not exist");
    }   
 }

public static int HighestVerbal()// Highest verbal score method 
{
int highest = verbalScore.get(0);
for (int i  = 0; i < verbalScore.size(); i++)
{
    if (highest < verbalScore.get(i))
        highest = verbalScore.get(i);
}
return highest;
}
public static int LowestMath()//Lowest math score method 
{
int lowest = mathScore.get(0);
for(int i = 1; i < mathScore.size(); i++)
{
    if (mathScore.get(i) < lowest)
        lowest = mathScore.get(i);
}
return lowest;
}
public static int UniqueTest() //Method that compares each student ID 
to see how many times the student took the test
{
int count = 0;
for (int i = 0; i < studentIDNumber.size()-1;i++)
{
    boolean found = false;

    for (int j = i+1; !found && j < studentIDNumber.size(); j++)
    {
        if (studentIDNumber.get(i).equals(studentIDNumber.get(j)))
        {
        found = true;
        count++;
        }
    }
}

return (studentIDNumber.size()-count);
}
public void toString(int studentID)// to String method 
 {
int rowNumber[]=new int[3];
int i = 0;
int highestMathScore = 0;
int highestVerbalScore = 0;
int bestTotalScore = 0;

    if(studentIDNumber.contains(studentID))
    {
        rowNumber[i] = studentIDNumber.indexOf(studentID);

        for(int j = 0; j < 10; j++)
        {

            if(studentIDNumber.get(studentIDNumber.indexOf(studentID) + j + 1 ) == studentID)
            {

                i++;
                rowNumber[i] = studentIDNumber.indexOf(studentID) + j + 1;                  
            }   


        }

        System.out.println("Evidence-Based Reading and Writing Section | Math Section | Total Score" );// Printing out the selected students scores
        for(i = 0; i < rowNumber.length; i++) 

        {
            if(rowNumber[i] >= 1 )

            {
                if(mathScore.get(rowNumber[i]) > highestMathScore)// if math score at  position i is greater than highest math score, than it is equal to that math score 
                {
                    highestMathScore = mathScore.get(rowNumber[i]);
                }
                if(verbalScore.get(rowNumber[i]) > highestVerbalScore)//if verbal score at position i is greater than highest verbal, than it is equal to that verbal score
                {
                    highestVerbalScore = verbalScore.get(rowNumber[i]);
                }
                if(totalScore.get(rowNumber[i]) > bestTotalScore)//if total score ar position i is greater than best total score, than it is equal to that score 
                {
                    bestTotalScore = totalScore.get(rowNumber[i]);
                }
                System.out.println(verbalScore.get(rowNumber[i]) + " | " + mathScore.get(rowNumber[i]) + " | " + totalScore.get(rowNumber[i]));
            }

        }
        System.out.println("Highest Math Score: " + highestMathScore);
        System.out.println("Highest Verbal Score: " + highestVerbalScore);
        System.out.println("Best Total Score: " + bestTotalScore);
        }
    else
    {
        System.out.println("Student ID does not exist");
    }

 }
}

任何帮助将不胜感激。

0 个答案:

没有答案