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;