猿问

使用.split()报错数组越界怎么处理?

public static void showTotalScoreAvg() throws IOException{
            double sum = 0;
            BufferedReader in = new BufferedReader(new FileReader(path));
            String studentScore;
            while((studentScore = in.readLine()) != null)
            {
                double mathScore = Double.parseDouble(studentScore.split("\t")[2]);
                double englishScore = Double.parseDouble(studentScore.split("\t")[3]);
                double computerScore = Double.parseDouble(studentScore.split("\t")[4]);
                double totalScore = mathScore + englishScore +computerScore;
                sum = sum + totalScore;
                count++;
                if(MinTotalScore > totalScore)
                {
                    MinTotalScore = totalScore;
                }
                if(MaxTotalScore < totalScore)
                {
                    MaxTotalScore = totalScore;
                }
                studentScore = in.readLine();
            }
            double totalScoreAvg = sum/(double)count;
            System.out.println("总成绩平均分为:"+ totalScoreAvg);in.close();
      }


动感光波biubiubiubiu
浏览 1774回答 3
3回答

小草1434

在写出这种取具体数组下标语句前,我有个建议,先判断下数组长度。 目前该问题描述的代码是因为该字符串不符合程序预期的长度[文件中体现的分数没有超过2个  /t]

动感光波biubiubiubiu

已解决
随时随地看视频慕课网APP

相关分类

Java
我要回答