-3

I need to use Buffered Reader and File Reader and format it under columns like this.

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

Here is how I want to calculate the GPA. Multiply the point value of the letter grade by the number of credit hours. The result is the quality points earned. Total the credit hours for the term; total the quality points for the term. Divide the total quality points by the total credit hours

Here is the information within the text file:

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

Here is my first attempt. I am new at this but I really would like to understand this because my professor just didn't explain it well enough for my understanding.

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;
Tay Lur
  • 11
  • 1
  • And you were planning on showing us your own code attempt to solve this with your question and ask an answerable specific a question, right? – Hovercraft Full Of Eels Feb 22 '16 at 03:03
  • 2
    I'm voting to close this question as off-topic because as written it's little more than a shameless homework dump. – Hovercraft Full Of Eels Feb 22 '16 at 03:04
  • I edited it with my attempt and the explanation of my scenario. This is an effort for better understanding before my practical to come in a few days. – Tay Lur Feb 22 '16 at 03:21
  • One problem with your code is that you're using `==` to check for String equivalence rather than use either the `equals(...)` or the `equalsIgnoreCase(...)` method. Understand that `==` checks if the two [i]object references[/i] are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Feb 22 '16 at 03:26
  • Another problem is that your posted code is unfinished and just ends, ends without closing braces, and thus is uncompilable and unrunnable. – Hovercraft Full Of Eels Feb 22 '16 at 03:26
  • Literally any help at all would be greatly appreciated. I am definitely not asking for someone to do it for me. I apologize for the formatting however; this is my first time using this site. In eclipse I do have the closing braces. I'll try to fix it with your advice. Thank you. – Tay Lur Feb 22 '16 at 03:27

1 Answers1

-1
import java.io.*;
import java.util.StringTokenizer;
public class p02gpa
{
public static void main(String[] args) throws IOException 
{
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=0;
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.print(name);
ctr++;
while(tok.hasMoreTokens())
{
 String nameS =tok.nextToken();
String letterGrade =tok.nextToken();
System.out.print(letterGrade);
hours =Integer.parseInt(nameS);
System.out.print(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;
}
System.out.println("");
}
}