Ok guys I'm almost done with my GPA program but I hit a snag. everything works fine but when I insert a letter grade the program doesn't read what number it corresponds to and sets double number to zero. Can some please let me know what I'm doing wrong.

``````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 totalHours = 0;
int totalHoursEarned = 0;
int hours;
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;

//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 hours each class was
Scanner input2 = new Scanner (System.in);
System.out.println("Please enter the number of hours this course was");
hours = input2.nextInt();
Scanner input3 = new Scanner(System.in);

//algorithm for finding the GPA
totalHours += hours;
totalHoursEarned += (hours * number);

}
//for loop ends

//GPA is calculated for all the students classes
gpa = totalHoursEarned / totalHours;

//GPA is printed to the screen
System.out.println(gpa);

}

}``````

## All 2 Replies

Consider that your series of "if" statements to evaluate the grade is outside of the loop where "Grade" is being entered.

Also be aware that you should use `.equals()` or `.equalsIgnoreCase()` to test string equivalence. Do not use `==` .

Consider that your series of "if" statements to evaluate the grade is outside of the loop where "Grade" is being entered.

Also be aware that you should use `.equals()` or `.equalsIgnoreCase()` to test string equivalence. Do not use `==` .

Thank you the .equals works I had no idea that that was the problem I much appreciate the assistance.

Be a part of the DaniWeb community

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