用逗号和空格读取文本文件以计算GPA

时间:2016-02-22 03:00:37

标签: java file-io bufferedreader filereader stringtokenizer

我需要使用Buffered Reader和File Reader,并在这样的列下进行格式化。

System.out.println("    Name               GPA  Classes        Hours/Grades" 
System.out.println("    ------------------ ---- -------- ------------------------ ");

以下是我想要计算GPA的方法。 将字母等级的点值乘以信用小时数。结果是获得的质量要点。                                                                                   该学期的学分总数;该术语的总质量点数。                                                                              将总质量点除以总学分

以下是文本文件中的信息:

Anagram,Anna,3/A,4/A,3/A,3/A,3/A
Boop,Betty,3/B,3/B,4/B,3/B,3/B,1/B
Chitwilly,Cindy,4/C,3/C,3/C,3/C,3/C
Downer,Debbie,3/D,4/D,4/D,3/D,1/D
Everhappy,Eva,4/B,3/B,3/D
Froghat,Freda,3/F,4/F,4/F,3/F
Getterdone,Gina,4/B,4/B,3/B,3/B,3/C
Hotentott,Heidi,3/D,4/D,4/C,3/C,1/B
Imagooden,Inga,4/B,4/B
Jitterbug,Jeannie,3/B,3/C,4/C,3/F,3/C,1/C
Keeper,Kendra,3/A,3/B,3/C
Lovermore,Lynda,4/A,4/B,4/C,4/D
Moocher,Minnie,3/B,3/C,3/D
Nussbaum,Nancy,4/C,4/D,4/F
Oglethorpe,Olga,3/B
Poppledooper,Penny,3/D,3/F
Qutie,Quinn,2/A,4/C,2/F

这是我的第一次尝试。我是新手,但我真的很想理解这一点,因为我的教授对我的理解并没有给予足够的解释。

import java.io.*;
import java.util.StringTokenizer;
    public class p02gpa {

        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub

                String         file = "p02-grades.txt";
                BufferedReader br   = new BufferedReader(new      FileReader(file));
                String          rec;

                String          fmt = "%2d: %-12s %3d %-8s %-20 %n";

                System.out.println("    Name               GPA  Classes        Hours/Grades");
                System.out.println("    ------------------ ---- -------- ------------------------ ");

                while((rec=br.readLine())!= null)
                    calcDisp(rec);

                br.close();

                  }
                  //----------------------------------------------------------------------------
                  public static void calcDisp(String rec)
                  {
                        StringTokenizer tok;
                        String          name;
                        String          fname;
                        String          lname;
                        double          ptValue;
                        double          classes;
                        double          qualtyPts;
                        double          hours;
                        double          gpa;
                        int             ctr=0;

                        tok     = new StringTokenizer(rec,",|/|");
                        lname   = tok.nextToken();
                        fname   = tok.nextToken();
                        name    = fname+ " "+lname;
                        System.out.println(name);
                        ctr++;

                        while(tok.hasMoreTokens())
                        {
                            String nameS =tok.nextToken();
                            String letterGrade =tok.nextToken();
                            System.out.println(letterGrade);
                            String hrsStr = tok.nextToken();
                            hours =Integer.parseInt(hrsStr);

                            System.out.println(hours);


                            if(letterGrade == "A")
                                ptValue = 4.0;

                            if(letterGrade == "B")
                                ptValue = 3.0;

                            if(letterGrade == "C")
                                ptValue = 2.0;

                            if(letterGrade == "D")
                                ptValue = 1.0;

                            if(letterGrade == "F")
                                ptValue = 0.0;



                              qualtyPts = ptValue * hours;
                              gpa = qualtyPts / hours;

1 个答案:

答案 0 :(得分:-1)

nul