Write a program to calculate a students GPA.
What I cant figure is how to calculate ( Total= credit * grade + credit * grade...etc)

or

(GPA = Total/grade +grade etc...)

If you run this program, you will see my calculation off by a lot.
Please help.

import java.util.Scanner;
 import java.io.*;


  public class GPA
  {

 private int total;
 private int credits;
 private int grades;
 private double gpa;
 private int gradeCounter1;
 private int creditCounter2;



  public static void main(String[] args)throws IOException
  {
 	Scanner key = new Scanner(System.in);
 	System.out.println("How many classes will you enter?");
 	int amount = key.nextInt();

 	key.nextLine();

 	int number;
 	int total = 0;
 	int credits = 0;
 	double gpa = 0;
 	int gradeCounter1 = 0;
 	int creditCounter2 = 0;
 	String className;




 	for
 		(number = 1;number <=amount;number++)
 		{

 		System.out.println("Enter Class Name and Course Number:");
 		 className = key.nextLine();

 		System.out.println("Enter Grade:" );

 		String grade;
 		grade = key.nextLine();
 		char letter = grade.charAt(0);
 		gradeCounter1++;

 		System.out.println("How many Credits is this class?");
 		 credits = key.nextInt();
 		 creditCounter2++;
 		 

 		key.nextLine();
 		System.out.println("Class\tGrade \tCredits");

 		System.out.println(className  + "\t"+ letter + "\t" + credits);

 		total += creditCounter2*gradeCounter1 ;
 		gpa  = total/(number*letter);

 		 if((letter =='Z') || (letter  == 'z'))
		 		{
		 		System.out.println("Try Again");
		 	    }
		 		else if((letter == 'A') || (letter == 'a'))
		 		{
		 		gpa = (gpa + 4.0) / number;
		 		}
		 		else if((letter == 'B') || (letter == 'b'))
		 		{
		 		gpa = (gpa + 3.0) / number;
		 		}
		 		else if((letter == 'C') || (letter == 'c'))
		 		{
		 		gpa = (gpa + 2.0) / number;
		 		}
		 		else if((letter == 'D') || (letter == 'd'))
		 		{
		 		gpa = (gpa + 1.0) / number;
		 		}
		 		else if((letter == 'F') || (letter == 'f'))
		 		{
		 		gpa = (gpa + 0.0) / number;
		 		}

		 		}





 		 System.out.println("Total grade points: " + total);
 		 System.out.println("Your GPA is a: " +gpa);


 	 }

	 }

Dani AI

Generated

Correct approach in one line: compute a running sum of quality points (numeric grade × course credits) and a running sum of credits, then divide: GPA = totalQualityPoints / totalCredits. Use double for quality-point totals and the final division to avoid integer truncation.

The posted code from has a few concrete mistakes that make results wildly wrong: it multiplies counters (gradeCounter1, creditCounter2) instead of the current course’s numeric grade by that course’s credits; it divides by a char (letter) (which uses the character’s integer code); it recalculates gpa inside each letter branch in a way that mixes prior results; and class-level fields are shadowed by local variables (confusing and unnecessary). The idea shown by — keep a credit sum and a quality-point sum, then divide — is the right pattern.

Example (compact, different from the code already posted):

double totalQualityPoints = 0.0;
int totalCredits = 0;

for (int i = 0; i < courseCount; i++) {
    String course = scanner.nextLine();
    char grade = scanner.next().toUpperCase().charAt(0);
    int credits = scanner.nextInt();
    scanner.nextLine(); // consume end-of-line

    double pts;
    switch (grade) {
        case 'A': pts = 4.0; break;
        case 'B': pts = 3.0; break;
        case 'C': pts = 2.0; break;
        case 'D': pts = 1.0; break;
        case 'F': pts = 0.0; break;
        default: pts = 0.0; /* handle invalid input appropriately */ 
    }

    totalQualityPoints += pts * credits;
    totalCredits += credits;
}

double gpa = (totalCredits == 0) ? 0.0 : totalQualityPoints / totalCredits;
System.out.printf("Total grade points: %.2f%nGPA: %.2f%n", totalQualityPoints, gpa);

Troubleshooting notes: validate grade input (handle A+/A- if needed), avoid shadowing variables, guard division by zero, and be careful mixing nextInt() and nextLine() (consume the newline). A simple test: a 3-credit A and a 3-credit B should yield total quality points 21.0 and GPA 3.50 (verify the program with that test).

Here is a small portion from a GPA calculator that I wrote. I believe it should be able to answer your question about performing the actual calculations.

hours = scan.nextDouble();
                        hoursSum += hours;        
                       
                        gradeLtrTemp = scan.next();
                        gradeLtr = gradeLtrTemp.toUpperCase();

                        if(gradeLtr.equals("A"))
                            gradeNum = 4.0;
                        else if(gradeLtr.equals("B"))
                            gradeNum = 3.0;
                        else if(gradeLtr.equals("C"))
                            gradeNum = 2.0;
                        else if(gradeLtr.equals("D"))
                            gradeNum = 1.0;
                        else if(gradeLtr.equals("F"))
                            gradeNum = 0.0;
                        else
                        {
                                System.out.println("Invalid grade.");
                                System.exit(0);
                        }
                
                        qualGrade += (gradeNum * hours);

 gpa = (qualGrade / hoursSum);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.