-1

I'm quite new to programming, and I was trying to find the next nearest palindromic date that will occur and I wrote the following code. But the problem that I'm facing is that it keeps on printing out the same exact thing again and again rather than increasing the date or the month. I've tried to debug this but I'm not seeing where I'm going wrong, can anyone please let me know how to do this!

 public static void main(String[] args) {

    Date date = new Date();
    int year = 1900+date.getYear();
    int month = 1+date.getMonth();
    int currDate = date.getDate();

    String currDate1 = formatter(currDate);
    String month1 = formatter(month);

    String theDate = currDate1+month1+year;

    while(palindromeChecker(theDate)){

        if(month==12) {
            year++;
            month=1;
        }

        if(year%4 == 0 && year%100 !=0 && year%400 == 0) {
            if (month == 2 && currDate == 29) {
                month++;
                currDate = 1;
            }
        }
        else {
            if (month == 2 && currDate == 28) {
                month++;
                currDate = 1;
            }
        }
        switch (month){
            case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                if(currDate==31) month++; currDate=1; break;
            case 4: case 6: case 9: case 11:
                if(currDate==30) month++; currDate=1; break;
        }

        currDate++;

        theDate = formatter(currDate)+formatter(month)+year;

        System.out.println(theDate);
    }

}

public static String formatter(int a){
    return new DecimalFormat("00").format(a);
}

public static boolean palindromeChecker(String a){
    String b = new StringBuilder(a).reverse().toString();
    if(a.equals(b)) return false;
    else return true;
}
saeed foroughi
  • 1,662
  • 1
  • 13
  • 25
ft1
  • 21
  • 4
  • 2
    All of your `month++;` calls are trapped within conditions that will never be true on the first run. Go through your code and look more closely at your cases. – sleepToken Feb 03 '20 at 17:46
  • Unrelated to your issue - your switch statement will work, but you could also switch on `month % 2` and then just handle the 0 and 1 case. – sleepToken Feb 03 '20 at 17:47
  • Even then at least the date variable should keep changing, but that doesn't happen as well – ft1 Feb 03 '20 at 17:51
  • `if(currDate==31) month++; currDate=1; break;` If you think that *both* of those conditions are applied to your `if` statement just because they are on the same line, you are mistaken. Better to use { } – sleepToken Feb 03 '20 at 17:57
  • You shouldn’t use `Date`. That class is long outdated and poorly designed. And even if you insisted, you should still stay far away from its `getYear`, `getMonth` and `getDate` methods, which have been deprecated for decades, literally, because they work unreliably across time zones. I recommend you use `LocalDate` and also `DateTImeFormatter` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Feb 03 '20 at 19:42

2 Answers2

0

The exercise is a good one, but sorry to say, you are doing it the wrong way. I figure that about the worst thing I could do would be solving the problem for you and posting complete and/or executable code. So I will try to guide you to the right way of solving the task. Please feel free to follow up in comments if you try and encounter problems.

java.time

First, use LocalDate for a date, not Date. Despite the name the Date class does not represent a date but a point in time (and at that point in time it is never the same date everywhere on Earth). In contrast a LocalDate is exactly a date without time of day and without time zone. Just what you need. Also the Date class was always poorly designed and is now long outdated.

For getting today’s date in your time zone use for example

    LocalDate date = LocalDate.now(ZoneId.systemDefault());

You can pass a different time zone to the now method if you want.

For converting the date into a string so that you can check whether it’s a palindrome use a DateTimeFormatter and the format method of LocalDate. You can use DateTimeFormatter.ofPattern() to construct the formatter. When all of this sounds vague or even incomprehensible, use your search engine to find examples and tutorials that will flesh it out a lot more, or check the documentation, use the links at the bottom.

Your way of checking whether a string is a palindrome is very elegant, so you should stay with it.

When one date turns out not to be a palindrome, use the plusDays method of LocalDate to add 1 day so you get the next day. LocalDate knows the next day, so you will not need to care about the number of days in each month nor about leap years, all of this is taken care of for you. Only remember that plusDays() returns a new LocalDate instance that you need to store into your variable.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    Thanks this was such a great advice. I have gone through the documentation and have reduced my code significantly! – ft1 Feb 05 '20 at 10:53
-1

It works fine for the first days, because its february. You have done it correctly for that special case.

Then for march, you keep resetting the day to 1 again and again in your month check inside the switch case statement. Try moving the line "currDate = 1;" inside the if-block.

The problem occurs, because you put both statements in a single line without braces.

if(currDate==30) month++; currDate=1;

Please always use braces for your blocks like this (that's also part of the coding guidelines):

if(currDate==30) {
    month++;
    currDate=1;
}
  • Thanks for this. It solved the problem and i could find a few other bugs as well which i fixed :) – ft1 Feb 03 '20 at 18:15