I just started learning java a week ago, and I'm trying to make my first program - a simple grade calculator for a class I'm in - and I can't get it to compile for the life of me. I've finally gotten it down to one error ("Comp202Grader.java:60: variable letterGrade might not have been initialized") but I have no idea why it's saying that! It's easier if I just show you:

public class Comp202Grader {
    public static void main(String[] args) {
        java.util.Scanner reader = new java.util.Scanner(System.in);
        System.out.println("Enter your name:");
        String studentName = reader.nextLine();
        System.out.println("Enter your 4 assignment grades (integers):");
        int assign1 = reader.nextInt();
        int assign2 = reader.nextInt();
        int assign3 = reader.nextInt();
        int assign4 = reader.nextInt();
        double assignAvg = (assign1+assign2+assign3+assign4)/4;
        System.out.println("Enter your midterm grade (decimal):");
        double midtermExam = reader.nextDouble();
        System.out.println("Enter your final exam grade (decimal):");
        double finalExam = reader.nextDouble();
        System.out.println(studentName + ", your summary is as follows:");
        System.out.println("Average assignment grade: " + assignAvg);
        double gradeWithMidterm = (assignAvg/100)*20 + (midtermExam/100)*20 + (finalExam/100)*60;
        System.out.println("Final grade (counting midterm): " + gradeWithMidterm);
        double gradeNoMidterm = (assignAvg/100)*20 + (finalExam/100)*80;
        System.out.println("Final grade (without midterm): " + gradeNoMidterm);
        double gradeNumber;
        if(gradeWithMidterm>gradeNoMidterm) {
            gradeNumber = gradeWithMidterm;
        } else {
            gradeNumber = gradeNoMidterm;
        }
        System.out.println("Your final percent score: " + gradeNumber);
        System.out.println("Your final letter grade: " + getLetterGrade(gradeNumber));
    }
    public static String getLetterGrade(double gradeNumber) {
        String letterGrade;
        if(gradeNumber>=85) {
            letterGrade = "A";
        }
        else if((85>gradeNumber)&&(gradeNumber>=80)) {
            letterGrade = "A-";
        }
        else if((80>gradeNumber)&&(gradeNumber>=75)) {
            letterGrade = "B+";
        }
        else if((75>gradeNumber)&&(gradeNumber>=70)) {
            letterGrade = "B";
        }
        else if((70>gradeNumber)&&(gradeNumber>=65)) {
            letterGrade = "B-";
        }
        else if((65>gradeNumber)&&(gradeNumber>=60)) {
            letterGrade = "C+";
        }
        else if((60>gradeNumber)&&(gradeNumber>=55)) {
            letterGrade = "C";
        }
        else if((55>gradeNumber)&&(gradeNumber>=50)) {
            letterGrade = "C-";
        }
        else if(gradeNumber<50) {
            letterGrade = "F";
        }
        return letterGrade;
    }
}

Any help would be greatly appreciated! I really hope to become an active member of this forum with time...

variable letterGrade might not have been initialized"

The compiler sees that there is a variable: letterGrade being used in the code that may not have been given an initial value. There is not an ending else clause following the chain of if/else if statements to guarantee the variable gets a value.
Give the variable a value when you define it.

Edited 4 Years Ago by NormR1

K, I changed line 32 to String letterGrade = "X"; and it worked - Thanks for the help! Was there a better way to do it?

Yes, that was good way, and other way was to remove the condition from line 57 and preferably make that an assertion at line 58, so it would check that your conditions are without holes between conditions (with proper test cases).

Initialize the variable letterGrade with null string.
like, String letterGrade=""; or String letterGrade=null;
And then see the output.It might be useful.

This question has already been answered. Start a new discussion instead.