线程"main"java.lang.ArrayIndexOutOfBoundsException中的异常

1 java arrays exception

我是编程的新手,在eclipse中运行一些新的代码时,我遇到了这个错误而且完全迷失了.

import java.util.Scanner;

public class Lab6
{
    public static void main(String[] args)
    {
        // Fill in the body according to the following comments
    Scanner in= new Scanner(System.in); 
        // Input file name
        String FileName=getFileName(in);
        // Input number of students
        int numOfStudents = FileIOHelper.getNumberOfStudents(FileName);
        Student students[] = getStudents(numOfStudents); 
        // Input all student records and create Student array and
        // integer array for total scores
        int[]totalScores = new int[students.length];
        for(int i=0; i< students.length; i++)
        {
            for(int j=1; j<4; j++)
            {
                totalScores[i]= totalScores[i]+students[i].getScore(j);
            }
        }
        // Compute total scores and find students with lowest and
        // highest total score
        int i;
        int maxIndex =0;
        int minIndex =0;
        for(i=0; i<students.length; i++);
        {
            if(totalScores[i]>=totalScores[maxIndex])
            {
                maxIndex=i;
            }
            else if(totalScores[i]<=totalScores[minIndex])
            {
                minIndex=i;
            }
        }
Run Code Online (Sandbox Code Playgroud)

问题似乎在行中if(totalScores [i]> = totalScores [maxIndex])

Dan*_*Dan 5

你有一个;在你的最后一个之后for,所以在for执行之后在每个步骤中没有附加命令,变量i将具有students.length超出数组边界的值.然后,{ ... }for后面的块用最终值执行一次i,导致异常.

删除它;,它应该工作.