Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default variable Shift might not have been initialized

    I want to get an hourly pay rate to display based on the shift number (integer) that the user inputs when prompted:

    Java Code:
    public double getPayRate()
       {
           int Shift;
           
           //payRate is supposed to be the double
           //Shift is supposed to be the int
           //Shift 1 = day shift
           //Shift 2 = night shift
                  
           if(Shift == 1)//ERROR: variable Shift might not have been initialized                            
           {
               payRate = 8.00;
           }
               
           else if(Shift == 2)
           {
               payRate = 10.00;
           }           
                       
          return payRate;
       }
    Is this syntax correct?

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: variable Shift might not have been initialized

    You have to set Shift equal to something. If it is a field of the class, it will default to 0. If it is declared inside a method like this, you must initialize it - that is, assign it a value. Right now it doesn't hold any values at all (at line 10).

    You also don't appear to declare payRate anywhere.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: variable Shift might not have been initialized

    Code Conventions for the Java Programming Language: Contents

    Variable names should start with a lowercase letter.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. variable may not have been initialized...
    By ajetrumpet in forum New To Java
    Replies: 6
    Last Post: 03-24-2013, 11:35 AM
  2. Variable may not have been initialized
    By gerchan in forum New To Java
    Replies: 1
    Last Post: 09-06-2012, 02:17 PM
  3. Variable not initialized?
    By seegee in forum New To Java
    Replies: 2
    Last Post: 07-15-2011, 02:00 PM
  4. Variable not initialized.
    By miss_peaches in forum New To Java
    Replies: 14
    Last Post: 04-27-2011, 09:28 AM
  5. variable name might not have been initialized
    By p0rnstar in forum New To Java
    Replies: 3
    Last Post: 01-26-2010, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •