# GPA calculator in Java

This is a program I typed up for a project pertaining to GPA calculation. However, I want to find an alternative to if then statements that are NOT conditionals, loops, or arrays. Is there an alternative solution to changing this format?

import java.util.Scanner;

public class Part1 {

public static void main(String args[]) {

String[] letters = {"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D", "F"};
// Creates strings above and the GPA values below
double[] grades = {4.33, 4.00, 3.67, 3.33, 3.00, 2.67, 2.33, 2.00, 1.67, 1.00, 0.00};
double credit1;
double credit2;
double credit3;
double credit4;
// Creates 4 credits
double totPtsClass1 = 0;
double totPtsClass2 = 0;
double totPtsClass3 = 0;
double totPtsClass4 = 0;
double totPts = 0;
double totalCredits = 0;
double gpa;
// Points in classes, GPA
System.out.println("Welcome to the UNG GPA Calculator!");
String message;

Scanner scan = new Scanner(System.in);
message = scan.nextLine();
message = scan.nextLine();
message = scan.nextLine();
System.out.println("Enter the number of your first class: ");
message = scan.nextLine();

Scanner console = new Scanner(System.in);

System.out.println("Please enter the number of credits of the class 1 (A number)");
credit1 = console.nextDouble();

System.out.println("Please enter the number of credits of the class 2 (A number)");
credit2 = console.nextDouble();

System.out.println("Please enter the number of credits of the class 3 (A number)");
credit3 = console.nextDouble();

System.out.println("Please enter the number of credits of the class 4 (A number)");
credit4 = console.nextDouble();

totPts = totPtsClass1 + totPtsClass2 + totPtsClass3 + totPtsClass4;
totalCredits = credit1 + credit2 + credit3 + credit4;
gpa = totPts / totalCredits;

}
}


I would like a solution other than the three above, but I'm unfamiliar with anything to substitute for all those if then statements.

Put the Grade-Score mapping in a HashMap and read it from there.

Do this once:

    Map<String, Double> gradeToScore = new HashMap<>();


Then you can use it as many times as needed:

    if(gradeToScore.containsKey(grade)) {
}else{
}


If you want to go several steps further in optimizing things for readability, you can get to something like this:

public class GradeCalculator {
//MAIN
public static void main(String args[]) {
calc.startInteraction();
}

//DATA CLASS
private static class Student{
public String firstName;
public String lastName;
public String major;
public String numFirstClass;
public final List<Double> credits = new ArrayList<>();
public final List<Double> points = new ArrayList<>();
}

//PROPERTIES & CONSTRUCTOR
private final Map<String, Double> gradeToScore = new HashMap<>();
}

//PUBLIC
public void startInteraction() {
System.out.println("Welcome to the UNG GPA Calculator!");

try(Scanner scan = new Scanner(System.in);){
Student student = new Student();

double gpa = calculateGpa(student);
}
}

//PRIVATE
private void loadStudentInfo(Student student, Scanner scan) {
student.firstName = scan.nextLine();
student.lastName = scan.nextLine();
student.major = scan.nextLine();
System.out.println("Enter the number of your first class: ");
student.numFirstClass = scan.nextLine();
}

private void loadCreditsAndPoints(Student student, Scanner scan, int classNumber) {
System.out.println("Please enter the number of credits of the class " + classNumber + " (A number)");
double credit = scan.nextDouble();

System.out.println("Please enter your grades for the class " + classNumber + " (Capital letters such as A,B+, C-)");

}else{
}
double classTotPts = gradeValue * credit;
}

private double calculateGpa(Student student) {
double totalCredits = getTotal(student.credits);
double totPts = getTotal(student.points);
double gpa = totPts / totalCredits;
return gpa;
}

private double getTotal(List<Double> doubles) {
double total = 0;
for(Double d:doubles) {
total += d;
}
}
}


And, I would prefer to stay away from streams and lambdas till you completely nail down core Java.

I know this is an old question but this answer could be helpful to others.

Since the letters and symbols we're dealing with are relatively close to each other in terms of character value, we can use that to our advantage by doing some simple math.

NOTE: This code is largely unchecked and rather verbose - optimize as desired

class Student {
private double points = 0.0;
private int credits = 0;
private final String name = "";

/**
* This returns the student's GPA as a double
*
* @return The calculated GPA
*/
public double getGPA() {
return ((points / credits) * 100) / 100;
}

/**
*
* @param credits     Total credit hours of the course
*/
}

// As we get closer to the letter 'E', the lower the total points
// The ASCII value of 'A" is 65, and the value for 'E' is 69
// 69 / 65 is 1, with a remainder of 4
// The remainder is what we're after, so use modulo
//
// For example, the program passes in the grade "C-" and the first
// character of that the string gradeString is C and we can get this
// value by using the charAt() method. Since Java is a 0-based index
// language we pass 0 into the charAt method to get the first character
// of the string. The decimal value of that character is 67. We know that
// the decimal value of the character 'E' is 69, so we can use the formula
// 69 % 67 which will give us 2.

// A little bit of error checking is required using these methods
if (c != 'X' &&
(gradeVal != 'D' && c != '-') &&
(gradeVal != 'A' && c != '+')) {
double point = -((c - (double) ',') / 3);
}
}

// Add the number of credits to our running total
this.credits += credits;

}

/**
* Get the student's name
*
* @return The student's name
*/
public String getName() {
return name;
}

/**
* The Student class
*
* @param name The student's name
*/
public Student(String name) {
this.name = name;
}
}

public class Program {
public static void main(String[] args) {
// Create a new Student object named student1
// student1's name is set with Robert
Student student1 = new Student("Robert");