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.
There are problems in your 'year' handling. The puzzle text says
Given a possibly ambiguous date "A/B/C", where A,B,C are integers between 0 and 2999, output the earliest possible legal date between Jan 1, 2000 and Dec 31, 2999 (inclusive) using them as day, month and year (but not necessarily in that order).
However, when I enter years between 1000 and 2000 into your program, it will report them verbatim, even though they aren't valid output.