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);
		System.out.println("Please enter your number of classes");
		int classes;
		classes = input.nextInt();
		String Grade = "";
		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
		if(Grade == "A")
			number = 4.0;
		else if(Grade == "A-")
			number = 3.67;
		else if(Grade == "B+")
			number = 3.33;
		else if(Grade == "B")
			number = 3.0;
		else if(Grade == "B-")
			number = 2.67;
		else if(Grade == "C+")
			number = 2.33;
		else if(Grade == "C")
			number = 2.0;
		else if(Grade == "C-")
			number = 1.67;
		else if(Grade == "D+")
			number = 1.33;
		else if(Grade == "D")
			number = 1.0;
		else if(Grade == "D-")
			number = 0.67;
		else if(Grade == "F")
			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();
			//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");
			Grade = input3.next();
			
			//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);
		
		
	

	}

	

}

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 == .

Edited 5 Years Ago by Ezzaral: n/a

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.

This article has been dead for over six months. Start a new discussion instead.