I\'m trying to solve the \"best before\" Spotify puzzle described on this page. Basically, with an input of three integers separated by slashes (e.g. 11/3/4) you\'re suppose
One bug: it accepts 2100/02/29. 2100 is not a leap year, so there is no 2011/02/29.
If I were you, I would use SimpleDateFormat
for parsing and validating (hint: lenient
parsing). It's much simpler, much intuitive and the code would be easier to read. (Don't Reinvent The Wheel)
Some other thoughts above.
Unnecessary assignments: return dateOK = false;
Just return with false
:
return false;
(The dataOK
variable is unnecessary in your case.)
public static void illegal(String dateInput){
System.out.println(dateInput + " is illegal");
System.exit(0);
}
Throw exceptions instead of System.exit()
.
In the getBestDate()
method the last two lines never run. They are dead code (since illegal()
calls System.exit()
):
}else{
illegal(dateInput);
}
Integer[] bestDate = {var1, var2, var3};
return bestDate;
If it's possible avoid static methods and fields. Lastly, it's a good problem to learn how to write unit tests.