java中的最终成绩计算器

时间:2017-11-21 19:19:49

标签: java parsing integer

这是一个个人项目,只是试图创建一个程序来计算我的最终成绩,据我所知,我认为我做的一切都正确,这是我的整个程序更新了工作代码:

FinalGradeCalculator:

import java.io.*;
import java.util.Scanner;
public class FinalGradeCalculator {
    public static final String readInFile = "MUSC110grades.txt";
    public static void main(String[] args) {
        try
        {
            Scanner readFile = new Scanner(new File(readInFile));
            String longString = "";
            while(readFile.hasNextLine())
            {
                longString = longString.concat(readFile.nextLine()+" ");
            }
            readFile.close();
            int indexQuiz = longString.indexOf("Quizzes");
            int indexConcertReports = longString.indexOf("Concert Reports");
            int indexMidterm = longString.indexOf("Midterm");
            int indexFinal = longString.indexOf("Final");
            int indexEnd = longString.indexOf("End");
            //Quiz
            String quiz = longString.substring(indexQuiz+8, indexConcertReports);
            String[] quizList = quiz.split(" ");
            int quizScores = 0;
            for(int i=0;i<quizList.length;i++)
                quizScores += Integer.parseInt(quizList[i]);
            //Concert Report
            String cR = longString.substring(indexConcertReports+16, indexMidterm);
            String[] cRList = cR.split(" ");
            int cRScores = 0;
            for(int i=0;i<cRList.length;i++)
                cRScores += Integer.parseInt(cRList[i]);
            //Midterm
            String midterm = longString.substring(indexMidterm+8, indexFinal-1);
            int midtermScores = Integer.parseInt(midterm);
            //Final
            String finals = longString.substring(indexFinal+6, indexEnd-1);
            int finalsScores = Integer.parseInt(finals);

            double quizAvg = Math.ceil(quizScores/quizList.length);
            System.out.println("Your Quiz Average is " + quizAvg);
            double cRAvg = Math.ceil(cRScores/cRList.length);
            System.out.println("Your Concert Report Average is " + cRAvg);
            double midtermGrade = Math.ceil(midtermScores);
            System.out.println("Your Midterm Grade is " + midtermGrade);
            double finalsGrade = Math.ceil(finalsScores);
            System.out.println("Your Finals Grade is " + finalsGrade);
            double totalGrade = Math.ceil(cRAvg*0.2 + quizAvg*0.35 + midtermGrade*0.2 + finalsGrade*0.25);
            System.out.println("Your Total Grade is " + totalGrade);

            if(totalGrade >= 90)
                System.out.println("Your final grade is A");
            else if(totalGrade >= 85)
                System.out.println("Your final grade is B+");
            else if(totalGrade >= 80)
                System.out.println("Your final grade is B");
            else if(totalGrade >= 75)
                System.out.println("Your final grade is C+");
            else if(totalGrade >= 70)
                System.out.println("Your final grade is C");
            else if(totalGrade >= 65)
                System.out.println("Your final grade is D+");
            else if(totalGrade >= 60)
                System.out.println("Your final grade is D");
            else
                System.out.println("your final grade is F");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

当我运行程序时,它给出了一个数字格式异常:java.lang.NumberFormatException: For input string: "70 "。这是文本文件:

MUSC110grades.txt:

Quizzes
100
100
100
100
100
100
100
100
100
100
100
0
0
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
0
0
100
100
0
0
100
100
100
100
100
100
100
100
100
100
100
100
Concert Reports
100
100
90
90
Midterm
72
Final
70
End

现在我已经查找了数字格式异常,但我不明白为什么数字50不是整数。我需要帮助解决这个问题,它可能与索引有关。我不确定。

1 个答案:

答案 0 :(得分:0)

我认为这是因为50之后有一个空格。你能检查一下吗?