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 6 of 6

Thread: Variable might not have been initialized?

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Variable might not have been initialized?

    Hey there, looking for help, we are building a grade calculator where we are trying to used a method within a method (code will explain) however i keep getting this variable not initialized and i dont know why heres my code:
    import java.util.Scanner;
     
    public class StudentGradesWeek7
    {
    	public static void main(String[] args)
    	{
     
    	String input = "";
          while (!input.equals("q"))
          {
    		Scanner in = new Scanner(System.in);
     
    		System.out.println("Enter student grade percentage or 'q' to exit:");
     
    		input = in.nextLine();
     
    		if (!input.equals("q"))
    		{
    		int inputMark = Integer.parseInt(input);
     
    		char g = getGrade(inputMark);
     
    		System.out.println("The students grade is:" + g);
    		}
     
     
    		}
    	}
     
    	private static char getGrade(int mark)
    	{
    	char grade;
    		if (mark >= 70)
    			{
    				grade = 'A';
    			}
    		else if (mark >= 65)
    			{
    				grade = 'B';
    			}
    		else if (mark >= 55)
    			{
    				grade = 'C';
    			}
    		else if (mark >= 45)
    			{
    				grade = 'D';
    			}
    		else if (mark >= 44)
    			{
    				grade = 'F';
    			}
     
    		return grade;
    	}
    }

    The program is supposed to ask what percentage grade the student received then issue them the grade, it will then ask again or ask them to enter q for the program to exit, any help is much appreciated!!

    Thank you, Nathan

    EDIT: its the last line: return grade; which is giving me the problem
    Last edited by n56; November 30th, 2010 at 02:17 PM.


  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: Variable might not have been initialized?

    Here's the problem: What if mark is less than 44? What should be returned then?

    In other words, not every path of execution results in grade being initialized.

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    n56 (November 30th, 2010)

  4. #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: Variable might not have been initialized?

    Give the char some default value like 'c'.

  5. #4
    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: Variable might not have been initialized?

    Quote Originally Posted by javapenguin View Post
    Give the char some default value like 'c'.
    Again, that's pretty bad advice. How does that answer the underlying problem? What if the grade is less than 44? Should "some default value like 'c'" be returned? No. The OP should fix the logic problem, not cover it up with hacky solutions.

    Edit- In fact, part of the error is a typo on the OP's fault. If he thinks about why I asked my original question, he'll figure it out. If he applies your "solution", he'll fail his assignment.

  6. The Following 2 Users Say Thank You to KevinWorkman For This Useful Post:

    JavaPF (November 30th, 2010), n56 (November 30th, 2010)

  7. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Variable might not have been initialized?

    I feel like a douche, ive got a More than or equal to 44, not less than or equal too, okay i changed that and it works, thank you very much, dammit i knew it would be something simple lol, thanks again.

  8. #6
    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: Variable might not have been initialized?

    Quote Originally Posted by KevinWorkman View Post
    Again, that's pretty bad advice. How does that answer the underlying problem? What if the grade is less than 44? Should "some default value like 'c'" be returned? No. The OP should fix the logic problem, not cover it up with hacky solutions.

    Edit- In fact, part of the error is a typo on the OP's fault. If he thinks about why I asked my original question, he'll figure it out. If he applies your "solution", he'll fail his assignment.


    Good point, what was I thinking?

Similar Threads

  1. How to use the same variable in various JPanel?
    By danielpereira in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 19th, 2010, 12:08 PM
  2. Not sure what type of variable to use.
    By mjpam in forum Java Theory & Questions
    Replies: 13
    Last Post: July 13th, 2010, 08:58 PM
  3. How do I set a static variable??
    By wingchunjohn in forum Object Oriented Programming
    Replies: 4
    Last Post: January 22nd, 2010, 04:36 AM
  4. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 06:28 AM
  5. Replies: 3
    Last Post: April 20th, 2009, 08:35 AM