Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: "Error: Variable might not have been initialized" mmm... I'm pretty sure it was

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default "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:

    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.");
    		}
    	}
    }
    Last edited by obstemp; October 17th, 2011 at 07:14 AM. Reason: Learning how to post properly


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: "Error: Variable might not have been initialized" mmm... I'm pretty sure it was

    Quote Originally Posted by javapenguin View Post
    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)

  5. #5
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: "Error: Variable might not have been initialized" mmm... I'm pretty sure it was

    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.
    Last edited by Tjstretch; October 20th, 2011 at 11:28 AM.

Similar Threads

  1. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  2. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  3. Is there an integer-type variable bigger than "long"?
    By bardd in forum Java Theory & Questions
    Replies: 2
    Last Post: September 3rd, 2010, 02:43 PM
  4. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  5. Replies: 3
    Last Post: April 20th, 2009, 08:35 AM