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: another easy one, variable not initialized

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default another easy one, variable not initialized

    I have been staring at this for longer than I care to admit, I dont know why it says the variable hold may not have been initialized? Looks initialized to me. You guys are the best any insight you may have would be awesome.

    1 error found:
    File: E:\ClassPractice1\ClassPractice1\Question1_IfState ment.java [line: 32]
    Error: The local variable hold may not have been initialized


    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
     
    //This program practices JOptionPane and an IF statement.
     
    public class Question1_IfStatement
    {
      public static void main(String[] args)
      {
        String inputString;
        double first;
        double second;
        double hold;
     
        DecimalFormat formatter = new DecimalFormat("#0.00");
     
        inputString = JOptionPane.showInputDialog ("What is the first number: ");
        first = Double.parseDouble (inputString);
     
        inputString = JOptionPane.showInputDialog ("What is the second number: ");
        second = Double.parseDouble (inputString);
     
        if (first >= second)
        {
          hold = (first * second) + (first * 2);
        }
        if (second > first)
        {
          hold = (first + second) + (second * 5);
        }
     
     
        JOptionPane.showMessageDialog(null, formatter.format(hold));
     
      System.exit(0);
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: another easy one, variable not initialized

    These are all local variables:
     public static void main(String[] args)
      {
        String inputString;
        double first;
        double second;
        double hold;
    None of them have been initialized.
    What variable is the one referenced in the error message? Has it been given a value before it is used?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: another easy one, variable not initialized

    Please initialize your variable first.
    it means to specify its initial value.
    double hold = 0;
    your trying print/access you variable hold without any initial value on it.
    you thought that you have set a value on it because of your statement
    if (second > first)
        {
          hold = (first + second) + (second * 5);
        }

    as you can see, that is a conditional statement,
    what if the condition is false?
    then you might never write anything on your variable right?
    that why it gives you a compilation error.
    initialize it first
    double hold = 0;

  4. The Following User Says Thank You to dicdic For This Useful Post:

    meangreen2003 (February 21st, 2014)

  5. #4
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: another easy one, variable not initialized

    Hi,

    "hold" is a local variable for main(String[]) method. Such variables are never initialized on its own. If you put these variables inside a constructure, then only it will be initialized to default variable.

    In your code, you are getting a compilation error "The local variable hold may not have been initialized" because compiler cannot process the statements (i.e. which initialize hold variable) inside if() statement while compiling. And when compiler checks for the statement "JOptionPane.showMessageDialog(null, formatter.format(hold));" it could not recognize the value for hold being initialized or assigned.

    If you put the statement "JOptionPane.showMessageDialog(null, formatter.format(hold));" inside if() block, your code will work fine. But, putting the same code in multiple if() block is not a good coding practise.

    So, there may be 2 approach:
    1. If you declaring local variable, then always initialize them while declaring.
    2. You can declare these variables globally and call a constructor to initialize them to desired value. Else default constructor will initialize those global variable to default values.

    Now, how to code the program is something which is completly upon the choice of programming taking into consideration best coding practices.

    Please let us know if you have any queries.
    Thanks and regards,
    Sambit Swain

  6. The Following User Says Thank You to Sambit For This Useful Post:

    meangreen2003 (February 21st, 2014)

  7. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: another easy one, variable not initialized

    You guys are awesome. Im guessing you are doing it for no pay as well which makes you twice as awesome. Setting a value of 0 for my variables worked great, Thanks again.

Similar Threads

  1. variable might not have been initialized!!
    By bassie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 4th, 2012, 12:39 PM
  2. variable might not have been initialized...help appreciated
    By neontiger in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2012, 05:58 PM
  3. Variable might not have been initialized
    By JavaGirl9001 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 7th, 2011, 11:24 AM
  4. variable might not have been initialized
    By SV25 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 25th, 2011, 10:58 AM
  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