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);
int classes;
classes = input.nextInt();
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
number = 4.0;
number = 3.67;
number = 3.33;
number = 3.0;
number = 2.67;
number = 2.33;
number = 2.0;
number = 1.67;
number = 1.33;
number = 1.0;
number = 0.67;
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();
Scanner input3 = new Scanner(System.in);

//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);
}
}
}``````

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 ;)

## 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 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.