Spotify puzzle task

I am trying to solve the Spotify puzzle "best" described on this page . Basically, when entering three integers separated by a slash (for example, 11/3/4), you should generate a result with the earliest possible date in the format 2011-03-04. If the date is not possible, it should return the original string, followed by "is illegal."

The idea of ​​my solution below was borrowed from a Python solution that I found for the same problem on github . When I submit this Python code, it is accepted. Not familiar with Python, this is my best attempt to do something similar with Java and without using any calendar functions, as seen in this solution posted here on stackoverflow.

When I submit my solution, however, I get an "Invalid answer" as the answer. Try as I could, I can not find any errors with this code. I feel like I have tried all possible combinations of inputs, and all my outputs come out correctly. Can anyone understand what I can lose?

Since I am relatively new to programming in general, feel free to also give tips on how code can be improved in general if you want. I am sure this may look noobish. Thank!

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class DateProggy3 {

    static int var1, var2, var3;
    static int slashPosition1, slashPosition2;
    static String yearString, monthString, dayString;

    public static void main(String[] args) throws IOException {
        String dateInput = readDate();
        splitInputToInts(dateInput);
        Integer[] dateArray = {var1, var2, var3};
        Arrays.sort(dateArray);
        Integer bestDate[] = getBestDate(dateArray, dateInput);
        convertDate(bestDate);
        printDate(bestDate);
    }

    public static String readDate() throws IOException {
        BufferedReader stdin = new BufferedReader
          (new InputStreamReader(System.in));
        String dateInput; 
        dateInput = stdin.readLine();
        return dateInput;
    }

    public static void splitInputToInts(String dateInput) {
        try {
        slashPosition1 = dateInput.indexOf('/');
        slashPosition2 = dateInput.lastIndexOf('/');
        var1 = Integer.parseInt(dateInput.substring(0, slashPosition1));
        var2 = Integer.parseInt(dateInput.substring(slashPosition1+1, slashPosition2));
        var3 = Integer.parseInt(dateInput.substring(slashPosition2+1, dateInput.length()));
        }catch (StringIndexOutOfBoundsException e){
            illegal(dateInput);
        }catch (NumberFormatException e){
            illegal(dateInput);
        }
    }

    public static void illegal(String dateInput){
        System.out.println(dateInput + " is illegal");
        System.exit(0); 
    }

    public static Integer[] getBestDate(Integer[] dateArray, String dateInput){
        var1 = dateArray[0];
        var2 = dateArray[1];
        var3 = dateArray[2];
        if (testDate(var1, var2, var3)){
            Integer[] bestDate = {var1, var2, var3};
            return bestDate;
        }
        else if (testDate(var1, var3, var2)){
            Integer[] bestDate = {var1, var3, var2};
            return bestDate;
        }
        else if (testDate(var2, var1, var3)){
            Integer[] bestDate = {var2, var1, var3};
            return bestDate;
        }
        else if (testDate(var2, var3, var1)){
            Integer[] bestDate = {var2, var3, var1};
            return bestDate;
        }
        else if (testDate(var3, var1, var2)){
            Integer[] bestDate = {var3, var1, var2};
            return bestDate;
        }
        else if (testDate(var3, var2, var1)){
            Integer[] bestDate = {var3, var2, var1};
            return bestDate;
        }else{
            illegal(dateInput);
        }
        Integer[] bestDate = {var1, var2, var3};
        return bestDate;
    }

    public static boolean testDate(int year, int month, int day){
        boolean leapYear = false;
        boolean dateOK;
        if (year > 100 && year < 2000){
            return dateOK = false;
        }
        if (year < 1000){
            year+=2000;
        }
        if (year < 0){
            return dateOK = false;
        }
        if (year % 4 == 0) {
            if (year % 100 == 0 && year % 400 != 0) {
                leapYear = false; 
            }
            leapYear = true;
        }else{
            leapYear = false;
        }
        if (month > 12 || month < 1){
            return dateOK = false;
        }
        switch (month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                if (day > 31 || day < 1){
                    return dateOK = false;
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                if (day > 30 || day < 1){
                    return dateOK = false;
                }
                break;
            case 2:
                int maxDay;
                if (leapYear){
                    maxDay = 29;
                }else{
                    maxDay = 28;
                }
                if (day > maxDay || day < 1){
                    return dateOK = false;
                }
        }
        return dateOK = true;
    }


    public static void convertDate(Integer[] dateArray){
        if (dateArray[0] < 1000){
            dateArray[0]+=2000; 
        }
        yearString = String.valueOf(dateArray[0]); 
        if (dateArray[1] < 10){ 
            monthString = "0" + dateArray[1];
        }else{
            monthString = String.valueOf(dateArray[1]);
        }
        if (dateArray[2] < 10){
            dayString = "0" + dateArray[2];
        }else{
            dayString = String.valueOf(dateArray[2]);
        }
    }

    public static void printDate(Integer[] dateArray){
        System.out.println(yearString + "-" + monthString +"-" + dayString);
    }
}

I am the one who asked this question, but cannot usually comment and answer the answers anymore, since I registered with stackoverflow and lost the original cookies or something like that.

Anyway, thanks to palacsint for your answer. I fixed the leap year problem and now my answer has finally been accepted!

Question about the last two lines in the method getBestDate(). I put them there only because the Eclipse IDE otherwise gives me the error "This method should return a result of type Integer []". He does not seem to be satisfied that ifall values ​​are in brackets . Is there any way around this? Thank you

+5
2

: 2100/02/29. 2100 , 2011/02/29.

, SimpleDateFormat (: lenient). , , . ( )

.

:   return dateOK = false;

false:

return false;

( dataOK .)

public static void illegal(String dateInput){
    System.out.println(dateInput + " is illegal");
    System.exit(0); 
}

System.exit().

getBestDate() . ( illegal() System.exit()):

}else{
    illegal(dateInput);
}

Integer[] bestDate = {var1, var2, var3};
return bestDate;

, . , , .

+1

"" .

"A/B/C", A, B, C 0 2999, 1 2000 31 2999 , , ( ).

, 1000 2000 , , .

0

All Articles