0

First of; I am very new to programming...

What i am trying to do is have a simple program which calculates your grade based on your marks, here is what i have right now:

 class Grade {

public static void main(String[]args){


char grade;


    int marks = 92;

    if(marks<60) 
    grade = 'F' ;

    else if(marks>61 && marks<69)
    grade = 'D';

    else if(marks>70 && marks<79)
    grade = 'C';

    else if(marks>80 && marks<89)
    grade = 'B';

    else if(marks>90 && marks<99)
    grade = 'A';

    else
    System.out.println("Bogus Grade");


    System.out.println("Your grade is " + grade);


}


}

(For now i'm just specifying the grade in the code, and will work on user input later)

While trying to compile the code I get this error:

  Grade.Java:30: error: variable grade might not have been initialized
                  System.out.println("Your grade is" + grade);
                                                       ^
2
  • Have you googled this? Commented Jul 15, 2014 at 21:13
  • Set the marks to 60 and you will see the problem. The grade variable needs to be set like char grade = 'I'; or something similar. Then fix your if/else blocks to handle the grades (e.g. <= instead of <). Commented Jul 15, 2014 at 21:15

4 Answers 4

3

Because it's possible to get through all the cases and have grade not be set.

Set a value to grade at the start:

char grade = 'Z';

Also make sure all cases are taken care of, and grade set all the time. In the final else, grade is not modified. You can do:

else {
    System.out.println("Bogus Grade");
    grade = 'Z';
}

Also, values of marks equal to 60, 70, 80, 90 and 100 are never taken into account. You should change the if lines to:

else if(marks>=60 && marks<69)
...
else if(marks>=70 && marks<79)
...
else if(marks>=80 && marks<89)
...
else if(marks>=90 && marks<=100)
...
else {
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

They also need to fix 70, 80, 90, and 100.
@gpojd Yes, just noticed. Edited my answer.
2

There are cases where grade isn't initialized. What if grade is 101? What if it's -1? In that case, the else block doesn't initialize grade, and Java won't allow a possible case where a local variable is not initialized before it is used.

You must explicitly initialize it, so initialize it to something when you declare it.

char grade = 'Z';

At the end, only print the grade if it was changed from 'Z':

if (grade != 'Z') {
   System.out.println("Your grade is " + grade);
}

Comments

0

Put char grade = '0'; at the start.

Also use brackets since you are using a lot of if-statements. They can get pretty confusing after a while.

Comments

0

Local variables are not initialised by default so you have to give them an initial value.

char grade = 'Z';

This only applies if you reach a point in your code where the variable could not have a value.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.