user2826974 user2826974 - 9 months ago 20
Java Question

int variable not initialized

private int getBirthdayYear(){
Random r1 = new Random();
int x = r1.nextInt(1000);
int year = 1000+ x;
return birthdayYear;
}
private String getBirthday(){
int year = getBirthdayYear();
Random r2 = new Random();
int y = r2.nextInt(11);
String monthStr;
int month = 1 + y;
switch (month) {
case 1: monthStr = "January";
break;
case 2: monthStr = "February";
break;
case 3: monthStr = "March";
break;
case 4: monthStr = "April";
break;
case 5: monthStr = "May";
break;
case 6: monthStr = "June";
break;
case 7: monthStr = "July";
break;
case 8: monthStr = "August";
break;
case 9: monthStr = "September";
break;
case 10: monthStr = "October";
break;
case 11: monthStr = "November";
break;
case 12: monthStr = "December";
break;
default: monthStr = "Invalid";
break;
}


Random r3 = new Random();
**int day;// day variable declared as int**
if(month == 1||month == 3||month == 5||month == 7||month == 8||month == 10||month == 12){
int z1 = r3.nextInt(30);
day = 1 + z1;
}
else if(month == 2||month == 4||month == 6||month == 9||month == 11){
int z2 = r3.nextInt(29);
day = 1 + z2;
}
else if(month == 2 & year%4 ==0){
int z3 = r3.nextInt(28);
day = 1 + z3;
}
else if(month == 2 & year%4 != 0){
int z4 = r3.nextInt(27);
day = 1 + z4;
}

**birthday = monthStr + " " + day + "th " + year;// error here. day variable is not initialized
return birthday;**
}


This is a code for generating a random birthdate, I'm not sure yet if it works. I'm still working on it. Right now, when I try to compile this code, it says that the day variable is not initialized yet. However, I already stated int day;. What should I do to fix this problem?

Answer Source

In Java, you can't use a variable that might be uninitialized. "Uninitialized" means the variable might not have been set to anything. If the compiler cannot prove that the variable is always initialized, it will produce an error.

int a;

// a is uninitialized

if(r.nextInt(10) < 2)
   a = 5;

// a has an 80% chance of still being uninitialized

System.out.println(a); // error: a might not be initialized.

In your case:

int day;
// day is uninitialized
if(month == 1||month == 3||...){
    int z1 = r3.nextInt(30);
    day = 1 + z1;
}
else if(month == 2||month == 4||month == 6||month == 9||month == 11){
    int z2 = r3.nextInt(29);
    day = 1 + z2;
}
else if(month == 2 & year%4 ==0){
    int z3 = r3.nextInt(28);
    day = 1 + z3;
}
else if(month == 2 & year%4 != 0){
    int z4 = r3.nextInt(27);
    day = 1 + z4;
}
// if the month wasn't between 1 and 12, day is still uninitialized

The compiler is not smart enough to detect that month is always between 1 and 12. The fix is to make sure day is always set to something, even if month is not in that range.

One way to do this:

int day;
// day is uninitialized
if(month == 1||month == 3||...){
    int z1 = r3.nextInt(30);
    day = 1 + z1;
}
else if(month == 2||month == 4||month == 6||month == 9||month == 11){
    int z2 = r3.nextInt(29);
    day = 1 + z2;
}
else if(month == 2 & year%4 ==0){
    int z3 = r3.nextInt(28);
    day = 1 + z3;
}
else if(month == 2 & year%4 != 0){
    int z4 = r3.nextInt(27);
    day = 1 + z4;
}
// THIS PART IS NEW
else
    day = -1; // this line will never actually run

Now the compiler can tell that, no matter what happens, day is always set to something after that if statement finishes.

Note that day = -1; never actually runs. It is only there so the compiler can prove that day always has a value.

Another way to fix the error is to change int day; to int day = -1; - for the same reason.