Results 1 to 7 of 7
- 01-26-2012, 07:45 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
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:
The error I get isJava 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); } }
Pay3.java:34: error: variable rate might not have been initialized
regularPay = FULL_WEEK * rate;
- 01-26-2012, 07:52 PM #2
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!
- 01-26-2012, 09:24 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
Re: Returner values from if statements
So, should I just implement a different loop?
- 01-26-2012, 11:45 PM #4
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: Returner values from if statements
Just initialize vaiable rate within declaration.
-
Re: Returner values from if statements
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
Do this:Java Code:int foo; // this is a variable declaration
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.Java Code:int foo = 0; // this is a variable declaration and initialization.
- 01-27-2012, 06:55 AM #6
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
-
Similar Threads
-
need to input values from a text file into an array and count values
By pds8475 in forum New To JavaReplies: 14Last Post: 01-22-2011, 02:36 PM -
How to get values for unselected checkbox values
By sarath13 in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 01-07-2011, 08:54 AM -
HashMap contains all values but doesn't show all values
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 05-10-2009, 11:35 PM -
Retaining DB values as well as Dynamically generated Values.. Help Needed !
By rajivjha in forum Advanced JavaReplies: 0Last Post: 05-22-2008, 10:53 AM -
Accessing boolean Values of another values in one class.
By a_iyer20 in forum Advanced JavaReplies: 4Last Post: 04-15-2008, 01:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks