0

I'm trying to print a String which was initialized within the if-else statement. But I'm having an error regarding dateStr may not have been initialized in the last line. Any suggestion? This is the code:

int currentDay = LocalDateTime.now().getDayOfMonth();
int currentMonth = LocalDateTime.now().getMonthValue();
int currentYear = LocalDateTime.now().getYear();
String dateStr;
if (currentDay < 10 && currentMonth < 10){
    dateStr = "0" + currentDay + "/0" + currentMonth + "/" + currentYear;
} else if (currentDay < 10 && currentMonth >= 10) {
    dateStr = "0" + currentDay + "/" + currentMonth + "/" + currentYear;
} else if (currentDay >= 10 && currentMonth >= 10){
    dateStr =  currentDay + "/" + currentMonth + "/" + currentYear;
}
System.out.println(dateStr);

9 Answers9

3

You did not initialize dateStr variable and the compiler is throwing the error because there is a possibility that none of your if blocks will be executed at runtime (because of the values for currentDay, currentMonth, the conditions are NOT met), then the dateStr variable is not pointing to anything, which is causing the error, so, there are two options to solve the issue:

Option (1): Initialize the dateStr variable

String dateStr = null;

Option (2): Add an else condition as shown below:

    if (currentDay < 10 && currentMonth < 10){       
        dateStr = "0" + currentDay + "/0" + currentMonth + "/" + currentYear;
    } else if (currentDay < 10 && currentMonth >= 10) { 
        dateStr = "0" + currentDay + "/" + currentMonth + "/" + currentYear;
    } else if (currentDay >= 10 && currentMonth >= 10){ 
        dateStr =  currentDay + "/" + currentMonth + "/" + currentYear;
    } else {
        dateStr="INVALID";
    }

I strongly suggest you make a practice to always initialize the local variables properly. The other point that you might be interested in is that you will NOT face this issue for class/instance variables which will automatically get initialized to default values (unlike local variables for which you need initialize).

Vasu
  • 21,832
  • 11
  • 51
  • 67
2

You are missing an else case without which your String may not have been initialized.

Otherwise initialize it to a default value, such as null or an empty string:

String dateStr = "";
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
2

initialize it to an empty string first:

String dateStr = "";

noobcoder
  • 323
  • 1
  • 3
  • 12
2
String dateStr= null;

That is because, what if none of the condition is fulfilled.

Zubair Nabi
  • 1,016
  • 7
  • 28
2

Yes, you must initialize this variable into the context of function and use into the else and if, like this:

String dateStr = "";
if (currentDay < 10 && currentMonth < 10){
    dateStr = "0" + currentDay + "/0" + currentMonth + "/" + currentYear;
} else if (currentDay < 10 && currentMonth >= 10) {
    dateStr = "0" + currentDay + "/" + currentMonth + "/" + currentYear;
} else if (currentDay >= 10 && currentMonth >= 10){
    dateStr =  currentDay + "/" + currentMonth + "/" + currentYear;
}
System.out.println(dateStr);

As you did it, you only are initialized the variable into the context of if or else if and that is the reason that compiler detect that the variable dateStris not initialized into the context of function.

Jorge Omar Medra
  • 978
  • 1
  • 9
  • 19
1

You declared a variable but didn't initialize it. To initialize you need to do this

    String datestr = "";
Faisal
  • 403
  • 4
  • 18
1

not all your paths add a value to the variable, do this

...
 else if (currentDay >= 10 && currentMonth >= 10){ 
        dateStr =  currentDay + "/" + currentMonth + "/" + currentYear;
    } else {
        dateStr = "it didn't work!";
}
...
Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
1

As @JeanLogeart stated, adding an else condition would remove your error. Although, I would create a padding function to mitigate the need for the conditionals like so:

private String padThatNumber(int number, int numberOfDigits){
    StringBuilder sb= newStringBuilder();
    sb.append(Integer.toString(number));
    while(sb.length()<numberOfDigits){
        sb.insert(0, "0");
    }

    return sb.toString();
}
CraigR8806
  • 1,584
  • 13
  • 21
0

What happens when currentDay >=10 && currentMonth<10?? Make sure to check all cases. Also try initializing the street to empty. String s ="";

Aleka
  • 242
  • 1
  • 15