Hey guys, I'm making a gpa calculator and almost have it completed but can't get it to read the grades and calculate the gpa properly. Any help would be great!

import java.util.Scanner;
 
public class StudentGPA {
 
 public static void main (String args[]){
 
  //User inputs the number of classes he/she has
  Scanner input = new Scanner (System.in);
  System.out.println("Please enter the amount of grades you have");
  int classes;
  classes = input.nextInt();
  String Grade = "";
  int totalCredits = 0;
  int totalCreditsEarned = 0;
  int credits;
  double gpa;
  double number=0;
  
    //String of if and else statements that set the number to the appropriate GPA
  if(Grade.equals("A")){
   number = 4.0;
  } else if(Grade.equals("A-")){
   number = 3.67;
  } else if(Grade.equals("B+")){
   number = 3.33;
  } else if(Grade.equals("B")){
   number = 3.0;
  } else if(Grade.equals("B-")){
   number = 2.67;
  } else if(Grade.equals("C+")){
   number = 2.33;
  } else if(Grade.equals("C")){
   number = 2.0;
  } else if(Grade.equals("C-")){
   number = 1.67;
  } else if(Grade.equals("D+")){
   number = 1.33;
  } else if(Grade.equals("D")){
   number = 1.0;
  } else if(Grade.equals("D-")){
   number = 0.67;
  } else if(Grade.equals("F")){
   number = 0;
  } else {
   number = 0; 
 
  //Loop that ends once the student has put information in on all his classes
  for(int count = 0; count < classes; count++)
  {
  
   //Reads the number of credits each class was
   Scanner input2 = new Scanner (System.in);
   System.out.println("Please enter the number of credits this class was (A number)");
   credits = input2.nextInt();
   //reads the letter grade using the String Grade prompt
   Scanner input3 = new Scanner(System.in);
   System.out.println("Please enter your grade for this class (Capital letters such as A, B+, C-)");
   Grade = input3.next();
 
   //algorithm for finding the GPA
   totalCredits += credits;
   totalCreditsEarned += (credits * number);
  }
 //for loop ends
  
  //GPA is calculated for all the students classes
  gpa = totalCreditsEarned / totalCredits;
 
  //GPA is printed to the screen
  System.out.println(gpa);
    }
  }
}

Recommended Answers

All 4 Replies

Suggestion, move that long if/else statement to its own method with return of double(example private static double credits(String Grade) ), when you read in grade call this method as totalCredits += credits(Grade); . See how that works for you ;)

I created the seperate method but isn't working just yet...what exactly do i do with the second part of totalCredits += credits(Grade);?

In your original code you used totalCredits += credits; that according to your come does algorithm for finding the GPA where earlier in the code you says that if/else statement String of if and else statements that set the number to the appropriate GPA
Therefore it was my understanding that if you want to get credits from given grade you need to run the grade through if/else statement that will provided you with a number that in my understanding is your credits.

If my assumption is not correct please correct me.

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.