"Error: Variable might not have been initialized" mmm... I'm pretty sure it was
This is the first time I have ever attempted to make a program. Java is completely new to me.
I thought the program I was working on would be too hard, but I'm stuck and don't know what it did wrong.
My compiler has been telling me that a variable may not have been initialized, and I don't know how to fix this. :((
Help please!
This is my code:
Code :
import java.util.Scanner;
/** This program prompts the user to enter a letter grade and prints its numeric value.*/
public class GradeNumber
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Please enter the letter grade: ");
String letterGrade = in.next();
String letter = letterGrade.substring(0,1);
String sign = letterGrade.substring(1);
double numericValue;
if (letter.equals("A") || letter.equals("B") || letter.equals("C") || letter.equals("D") || letter.equals("E"))
{
if (letter.equals("A"))
{
numericValue = 4;
}
else if (letter.equals("B"))
{
numericValue = 3;
}
else if (letter.equals("C"))
{
numericValue = 2;
}
else if (letter.equals("D"))
{
numericValue = 1;
}
else
{
numericValue = 0;
}
}
else
{
System.out.print("Error: The letter should be capitalized and be A, B, C, D or F only.");
}
if (sign.equals("+") || sign.equals("-") || sign.length()== 0)
{
if (sign.equals("+"))
{
if (letter.equals("A"))
{
System.out.print("The numeric value is: " + numericValue);
}
else
{
numericValue = numericValue + 0.3;
System.out.print("The numeric value is: " + numericValue);
}
}
else if (sign.equals("-"))
{
numericValue = numericValue - 0.3;
System.out.print("The numeric value is: " + numericValue);
}
else
{
System.out.print("The numeric value is: " + numericValue);
}
}
else
{
System.out.print("Error: If the grade has a sign, it should be + or - only.");
}
}
}
Re: "Error: Variable might not have been initialized" mmm... I'm pretty sure it was
Please use highlight tags! I added them for you this time, but every "read this before posting" here requested you do so.
Where are you getting the error? What is the text of the entire error? To what variable is it referring?
Hint: When do you initialize the variable? If it's in an if statement, what happens if that if statement is not true?
Re: "Error: Variable might not have been initialized" mmm... I'm pretty sure it was
1.) Why does it say that it should have been initialized to A,B,C,D, or F only in the error message yet check to see if it's initialized to A,B,C,D, or E in the if statement? Was that a typo?
Also, you need to have a
{ after your class declaration and also an ending one at the end of the class, which in this case appears to be when the program is done.
2.) Which variable is it? Is it numericvalue?
Re: "Error: Variable might not have been initialized" mmm... I'm pretty sure it was
Quote:
Originally Posted by
javapenguin
Also, you need to have a
{ after your class declaration and also an ending one at the end of the class, which in this case appears to be when the program is done.
Advice completely astray from the problem at hand, and incorrect I might add, which can lead the original poster completely astray (yet once again).
obstemp, I recommend reading the following: Initializing Fields (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Re: "Error: Variable might not have been initialized" mmm... I'm pretty sure it was
Code Java:
if (letter.equals("A") || letter.equals("B") || letter.equals("C") || letter.equals("D") || letter.equals("E"))
{
//*Code not Included*
}
Are you sure you want to check for those letters?
EDIT, that was already answered ;)... and the link I would normally give is already posted.