Results 1 to 7 of 7
  1. #1
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Returner values from if statements

    I am trying to create an application to calculate the pay of an employee based on their skill level. I have created some if statements that determine the pay rate depending their skill level. I keep getting errors saying that a variable has not been initialize. I need help clearing that error. Here is my code:

    Java Code:
    import java.util.*;
    import javax.swing.*;
    
    public class Pay3
    {
    	public static void main(String[] args)
    	{
    		double rate;
    		double hoursWorked;
    		double regularPay;
    		double overtimePay;
    		int skillLevel;
    		final int FULL_WEEK = 40;
    		final double OT_RATE = 1.5;
    		Scanner keyboard = new Scanner(System.in);
    		
    		System.out.print("What is your skill level?");
    		skillLevel = keyboard.nextInt();
    		if(skillLevel == 1)
    			rate = 17.00;//how do I format this in order to return the rate value??
    			else
    				if(skillLevel == 2)
    					rate = 20.00;
    				else
    					if(skillLevel == 3)
    						rate = 22.00;
    		else
    			System.out.println("Invalid skill level.");
    		
    		System.out.print("How many hours did you work this week?");
    		hoursWorked = keyboard.nextDouble();
    		if(hoursWorked > FULL_WEEK)
    		{
    			regularPay = FULL_WEEK * rate;
    			overtimePay = (hoursWorked - FULL_WEEK) * OT_RATE * rate;
    		}
    		else
    		{
    			regularPay = hoursWorked * rate;
    			overtimePay = 0.0;
    		}
    		System.out.println("Regular pay is " +
    			regularPay + "\nOvertime pay is " + overtimePay);
    	}
    	
    }
    The error I get is

    Pay3.java:34: error: variable rate might not have been initialized
    regularPay = FULL_WEEK * rate;

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Returner values from if statements

    That error is showing up because you only initialize your rate variable inside that if statement. What happens if that if statement evaluates to false?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: Returner values from if statements

    So, should I just implement a different loop?

  4. #4
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Returner values from if statements

    Just initialize vaiable rate within declaration.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Returner values from if statements

    Quote Originally Posted by gabrielpr12 View Post
    So, should I just implement a different loop?
    The code you've posted contains no loops, so your question above doesn't make sense.

    What you should do is as noted by diamonddragon above, initialze your variable with some default value. What value do you want it to have when everything starts, or if none of the if conditions are true? Then where you declare the variable, assign that value to it.

    In other words instead of

    Java Code:
    int foo; // this is a variable declaration
    Do this:

    Java Code:
    int foo = 0; // this is a variable declaration and initialization.
    Though note that the value of 0 was used just for illustrative purposes. You will have to decide what should be the best initial value to use.

  6. #6
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: Returner values from if statements

    Quote Originally Posted by Fubarable View Post
    The code you've posted contains no loops, so your question above doesn't make sense.

    What you should do is as noted by diamonddragon above, initialze your variable with some default value. What value do you want it to have when everything starts, or if none of the if conditions are true? Then where you declare the variable, assign that value to it.

    In other words instead of

    Java Code:
    int foo; // this is a variable declaration
    Do this:

    Java Code:
    int foo = 0; // this is a variable declaration and initialization.
    Though note that the value of 0 was used just for illustrative purposes. You will have to decide what should be the best initial value to use.
    Thanks, that makes sense. Its working now.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Returner values from if statements

    You're welcome -- glad you've got it working.

Similar Threads

  1. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  2. How to get values for unselected checkbox values
    By sarath13 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-07-2011, 08:54 AM
  3. HashMap contains all values but doesn't show all values
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-10-2009, 11:35 PM
  4. Replies: 0
    Last Post: 05-22-2008, 10:53 AM
  5. Replies: 4
    Last Post: 04-15-2008, 01:04 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
  •