954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

GPA Calculator

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

	}

	

}
Prisms
Light Poster
27 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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.

Prisms
Light Poster
27 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: