数组索引越界Java

时间:2020-09-23 14:41:26

标签: java arrays file indexoutofboundsexception

我有一个数组,该数组的大小应为3,总共要进行3次潜水。它从double []中提取了7个分数。我使用了一个测试方案来查看diveScore()方法是否有效并且计算正确。但是,当我尝试从文件中读取数字并将它们放入数组中时,保存每次潜水的已计算潜水得分的scoreArray无法存储数据。我的硬编码测试用例中的scoreArray正常工作。这是控制台运行时的异常。我试图看一下错误状态所在的行,但是我在主文件中注释掉的测试数据对于每次潜水的3组分数均能正常工作。我想念什么?每个for循环中都没有<=,就像所有人都向我展示了许多文章一样。

例外:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 
Index 3 out of bounds for length 3
    at Diver.diveScore(Diver.java:33)
    at Main.readFile(Main.java:74)
    at Main.main(Main.java:10)

主文件:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {
        Diver [] divers = new Diver[25];
        
        int numDivers = readFile(divers);
        System.out.println(numDivers);
        /*Diver d = new Diver("Frank Stalteri", "Middlesex County College");
        double [] scores = {6.2, 3.5, 8, 7.3, 6, 9.1, 4.8};
        double [] scores2 = {7.4, 5.4, 8, 3.7, 2.5, 7.8, 4};
        double [] scores3 = {4.6, 7.2, 7.3, 1.4, 3.6, 8, 4.7};
        
        d.diveScore(1, scores, 2);
        d.diveScore(2, scores2, 4);
        d.diveScore(3, scores3, 3);
        
        System.out.println(d.toString());
        */
    }
    public static int readFile(Diver [] divers) throws FileNotFoundException {
        File f = new File("divers.dat");
        Scanner kb = new Scanner(f);
        Diver d;
        
        int count = 0;
        int diveNum = 1;
        
        while (kb.hasNextLine()) {
            String name = kb.nextLine();
            String school = kb.nextLine();
            String dive1 = kb.nextLine();
            String dive2 = kb.nextLine();
            String dive3 = kb.nextLine();
            
            //String [] of size 8 (inlcudes the difficulty #)
            String [] scores1 = dive1.split("\\s+");
            String [] scores2 = dive2.split("\\s+");
            String [] scores3 = dive3.split("\\s+");
            
            //gets the difficulty from String [] then parses
            double diff1 = Double.parseDouble(scores1[scores1.length - 1]);
            double diff2 = Double.parseDouble(scores2[scores2.length - 1]);
            double diff3 = Double.parseDouble(scores3[scores3.length - 1]);
            
            //make new double [] of size 7
            double [] scores1D = new double[scores1.length - 1];
            double [] scores2D = new double[scores2.length - 1];
            double [] scores3D = new double[scores3.length - 1];
            
            //loops through String [] and casts numbers without difficulty number
            for (int i = 0; i < scores1D.length; i++) {
                scores1D[i] = Double.parseDouble(scores1[i]);
                //System.out.println(scores1D[i]);
            }
            for (int i = 0; i < scores2D.length; i++) {
                scores2D[i] = Double.parseDouble(scores2[i]);
                //System.out.println(scores2D[i]);
            }
            for (int i = 0; i < scores3D.length; i++) {
                scores3D[i] = Double.parseDouble(scores3[i]);
                //System.out.println(scores3D[i]);
            }
            
            d = new Diver(name, school);
            divers[count] = d;
            count++;
            
            d.diveScore(diveNum, scores1D, diff1);
            diveNum++;
            d.diveScore(diveNum, scores2D, diff2);
            diveNum++;
            d.diveScore(diveNum, scores3D, diff3);
            
            //System.out.println(d.toString());
        }
        kb.close();
        return count;
    }
    public static void printDivers(Diver [] divers, int numDivers) {
        System.out.println("All Divers\n");
        for (int i = 0; i < divers.length; i++) {
            if (divers[i] != null) {
                System.out.println(divers[i]);
            }
        }
    }
}

潜水文件:

public class Diver {
    
    private String name;
    private String school;
    private double [] scoreArray = new double [3];
    private double totalScore;

    Diver() {
        
    }
    Diver(String name, String school) {
        this.name = name;
        this.school = school;
    }
    //loop through score array and calculate the total score for each dive attempt
    public double [] diveScore(int diveNum, double [] scores, double difficulty) {
        double min = min(scores);
        double max = max(scores);
        double total = 0;
        
        for (int i = 0; i < scores.length; i++) {
            total += scores[i];
        }
        total -= max;
        total -= min;
        total *= difficulty;
        
        scoreArray[diveNum - 1] = Math.round(total * 100.0) / 100.0;
        return scoreArray;
    }
    //finds smallest score in array of scores
    private double min(double [] scores) {
        java.util.Arrays.parallelSort(scores);
        
        double min = scores[0];
        return min;
    }
    //finds largest score in array of scores
    private double max(double [] scores) {
        java.util.Arrays.parallelSort(scores);
        
        double max = scores[scores.length - 1];
        return max;
    }
    //calculates total of the 3 dives
    public double totalScore() {
        for (int i = 0; i < scoreArray.length; i++) {
                totalScore += scoreArray[i];
        }
        return totalScore;
    }
    public String toString() {
        String str = name + ", " + school + ": " + totalScore() + "\n" + "Dive 1: " + scoreArray[0] + "\n" + "Dive 2: " + scoreArray[1] + "\n" + "Dive 3: " + scoreArray[2] + "\n";
        return str;
    }
public boolean equals(Diver d) {
    boolean value = true;
        if (d == null || !(d instanceof Diver)) {
            value = false;
            return value;
        }
        Diver diver = (Diver) d;
        if (this.totalScore == d.totalScore) {
            value = true;
            return value;
        }
        return value;
    }
}

1 个答案:

答案 0 :(得分:0)

您的第一个潜水员必须从0开始
Main> readFile> while循环

int diveNum = 0;
相关问题