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

Thread: variable might not be initialized problem. Can someone tell me whats wrong?

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default variable might not be initialized problem. Can someone tell me whats wrong?

    when I compile this program it tells me that result might not have been initialized. i have the return types as return result shouldnt this work? can someone tell me how to fix this?






    // CheckAnswer.java - This program checks to see if the answer to a test item is correct.
    // Input: None.
    // Output: Correct or incorrect.
    import javax.swing.*;

    public class CheckAnswer
    {
    public static void main(String args[])
    {
    int intAnswer = 2;
    String stringAnswer = "Abraham Lincoln";
    boolean boolAnswer = false;
    String result;


    checkQuestion(intAnswer);
    System.out.println("The int answer is " + result);

    checkQuestion(stringAnswer);

    System.out.println("The String answer is " + result);


    checkQuestion(boolAnswer);

    System.out.println("The boolean answer is " + result);

    System.exit(0);

    } // End of main() method.



    public static String checkQuestion(int answer)
    {
    String result;
    if(answer==answer)

    {
    result="correct";


    }
    return result;


    }



    public static String checkQuestion(String answer)
    {

    String result;

    if(answer.compareTo(answer)==0)
    {
    result="correct";

    }

    return result;


    }


    public static String checkQuestion(boolean answer)
    {
    String result;
    if(answer=false)
    {
    result="correct";

    }

    return result;

    }




    } // End of CheckAnswer class.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: variable might not be initialized problem. Can someone tell me whats wrong?

    public int doStuff(boolean bool) {
        int value;
        if(bool) {
            value = 10;
        }
        return value;
    }
    What value should be returned when false is passed in as a parameter? You have the same issue in your code.
    Improving the world one idiot at a time!

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

    NewAtJava (November 11th, 2011)

  4. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: variable might not be initialized problem. Can someone tell me whats wrong?

    Quote Originally Posted by Junky View Post
    public int doStuff(boolean bool) {
        int value;
        if(bool) {
            value = 10;
        }
        return value;
    }
    What value should be returned when false is passed in as a parameter? You have the same issue in your code.
    Doesn't java initialize all primitive data types itself?
    Well, talking about String, that must be initialized.
    Junky's post is valueable to you as a logic, if you consider value as a String variable.

  5. The Following User Says Thank You to Mr.777 For This Useful Post:

    NewAtJava (November 11th, 2011)

  6. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: variable might not be initialized problem. Can someone tell me whats wrong?

    The difference is that a local variable in a method is different from a class variable.
    The local variable could go on the stack and not be initialized when the method is called.
    The class level variable gets a value when the class is created.

    Write a small test program and see if the above is true.

  7. The Following User Says Thank You to Norm For This Useful Post:

    NewAtJava (November 11th, 2011)

Similar Threads

  1. Replies: 4
    Last Post: October 20th, 2011, 11:26 AM
  2. Whats wrong here?
    By Java Sucks in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 8th, 2011, 08:33 PM
  3. variable might not have been initialized
    By SV25 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 25th, 2011, 10:58 AM
  4. whats wrong?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2011, 02:35 PM
  5. Variable might not have been initialized?
    By n56 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 30th, 2010, 03:03 PM