1

Receiving the error:

Pay.java:81: error: variable hourlyWage might not have been initialized JOptionPane.showMessageDialog(null, hourlyWage);

for the following code. The double hourlyWage is declared, these statements are within the main argument. Not seeing what I'm missing, do you?

if(skillInt == 1){
    hourlyWage = 17.00;
} else if(skillInt == 2){
    hourlyWage = 20.00;
} else if(skillInt == 3){
    hourlyWage = 22.00;
}
JOptionPane.showMessageDialog(null, hourlyWage);
Zong
  • 6,160
  • 5
  • 32
  • 46
ewbrowning
  • 107
  • 1
  • 10
  • 1
    Not enough information. Produce a minimum working example that only includes the relevant parts (not your entire baseline). – Engineer2021 May 05 '14 at 14:30
  • possible duplicate of [Variable might not have been initialized error](http://stackoverflow.com/questions/2448843/variable-might-not-have-been-initialized-error) – Avi May 05 '14 at 14:31
  • abracadabra, it works. thanks for the help – ewbrowning May 05 '14 at 14:33
  • It might not be going into any of your IF ELSE ladder.From where you are sending the value of `skillInt` – NeverGiveUp161 May 05 '14 at 14:34

5 Answers5

9

The message states, that maybe (if skillInt is not in {1, 2, 3}) you use variable (hourlyWage), which is not initialized. Either firstly initialize it with some value:

hourlyWage = 0;
if (skillInt == 1)
    //...

or add else block:

//...
else if (skillInt == 3)
    hourlyWage = 22.0;
else
    hourlyWage = 0;

If the only possible values for skillInt is {1, 2, 3}, then you maybe want to move third if block with just else:

//...
else {
    hourlyWage = 22.0;
}

By the way, the more beautiful way to do this is switch operator:

switch (skillInt) {
    case 1 : hourlyWage = 17.0;
        break;
    case 2 : hourlyWage = 20.0;
        break;
    case 3 : hourlyWage = 12.0;
        break;
    default : hourlyWage = Double.POSITIVE_INFINITY;
}
Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
2

You need to initiate hourlyWage:

double hourlyWage = 0;     // or some other value
if(skillInt == 1) {
// ...
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
2

hourlyWage gets its value inside an if Statement. So it is possible, that this line is skiped.

add a default initialisation before the if to it like:

double hourlyWage = 0.0;

Only declaring the variable without a value is only possible if in all execution branches a value is written to the variable before the variable is read. So adding a value in an else branch would also fix your problem. Adding a value only in an else if branch would be similar to just using the if branch.

Simulant
  • 19,190
  • 8
  • 63
  • 98
1

The compiler's indicating that it believes that there's a possible code path that means that the variable's used without being initialised.

The preferred fix is to initialise it to a default value at creation but failing that, ensure that your if/else chain ends in an unconditional else block where you set the default value.

Gwyn Evans
  • 1,361
  • 7
  • 17
0

In your code, if skillInt is not equal to 1, 2, or 3, hourlyWage is not assigned at all. If there is a suitable default value for hourlyWage that works for every possible value of skillInt except 1, 2, or 3, you can set that either by doing so above the if/else (double hourlyWage = XX.XX;) or by setting it in an unconditional else branch: } else { hourlyWage = XX.XX; }. (Substitute the default value for the XXs.)

I'm making this post because the other four answers here neglect something important.

If (as seems more likely) there is no default value for hourlyWage that is correct to use when skillInt has a value that is not understood, then blindly setting hourlyWage to 0 or some other invalid value and potentially plowing ahead with further processing cannot produce correct results, and can only exacerbate what is already an error. If the method cannot make use of the current value of skillInt, the only correct thing to do is abort processing such as by throwing an exception:

if (skillInt == 1) {
    hourlyWage = 17.00;
} else if (skillInt == 2) {
    hourlyWage = 20.00;
} else if (skillInt == 3) {
    hourlyWage = 22.00;
} else {
    throw new IllegalArgumentException("Skill value out of range");
}

Also note that floating-point data types are not precise and are never suitable for currency values in real-world applications. In a real-world application, you must use the BigDecimal type for currency values.

Boann
  • 48,794
  • 16
  • 117
  • 146