0

This is an exercise to use Zeller's algorithm.

When I run the program, and after I enter a value for the first user input ("Enter a year: ") and hit "return", the program prints the third user input prompt as well as the second, like this:

Enter a year: 1996

Enter the name of a month: 

Enter in a date in a month. Ensure that the date
is within the range of days (eg. Do  not enter '30' if your month is February):
enter code here

Is this something to do with the third prompt being inside a do-while loop? Thanks in advance!

Here's the whole program:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    
    //User enters a year.
    System.out.print("Enter a year: ");
    int year = input.nextInt();
    int k = (year % 100);
    int j = (year / 100);
    
    System.out.println(); //New line for legiblity on the console;
    
    //User enters a month.
    System.out.println("Enter the name of a month: ");
    String userMonth = input.nextLine();
    String month = userMonth;
    int m = 0; // This will be reassigned, in the switch statement, with the integer value of the user's month.
**enter code here**
    switch (month) {
        case "March": m = 3;
                break;
        case "April": m = 4;
                break;
        case "May": m = 5;
                break;
        case "June": m = 6;
                break;
        case "July": m = 7;
                break;
        case "August": m = 8;
                break;
        case "September": m = 9;
                break;
        case "October": m = 10;
                break;
        case "November": m = 11;
                break;
        case "December": m = 12;
                break;
        case "January": m = 13;
                break;
        case "February": m = 14;    
    } 
    
    System.out.println(); //Just for ease of legibility on the console.
    
    /* Declaring an integer variable for the user's date,
     * for when the date is confirmed to be valid within monthly parameters.
     */
    int q;
    int date = 0; //This will be reassigned, in the do-while loop, with the user's date.
    
    do {
        System.out.print("Enter in a date in a month. Ensure that the date\n"
                + "is within the range of days (eg. Do  not enter '30' if your month is February): ");
        q = input.nextInt();
    
            if ((m == 4 || m == 6 || m == 9 || m == 11) && q <= 30 && q >= 1) {
                date = q;
                break;
            }
            else if ((m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12 
                    || m == 13 || m == 14) && q <= 31 && q >= 1) {
                date = q;
                break;
            }
            else if (m == 2 && q <= 28 && q >= 1 || m == 2 && q <= 29 && q >= 1 && isLeapYear(year)) {
                date = q;
                break;
            }
            else {
                System.out.println("Date is outside of range for this month. Please try again.");
            }
                
    } while (date != q);
        
    //Zeller's formula
    int h = ((q + (( (26 * (m + 1))) / 10) + k + (k / 4) + (j / 4) + (j * 5)) % 7);
    
    String dayOfTheWeek;
        switch (h) {
        case 0: dayOfTheWeek = "Saturday";
                break;
        case 1: dayOfTheWeek = "Sunday";
                break;
        case 2: dayOfTheWeek = "Monday";
                break;
        case 3: dayOfTheWeek = "Tuesday";
                break;
        case 4: dayOfTheWeek = "Wednesday";
                break;
        case 5: dayOfTheWeek = "Thursday";
                break;
        case 6: dayOfTheWeek = "Friday";
                break;
        case 7: dayOfTheWeek = "Saturday";
    }
        
    System.out.println("The day of the week on day number " + q + " in the year " + year + " was " + h);
}

//A method to check if the user-entered year is a leap year.
public static boolean isLeapYear(int year) {
    if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
        return true;
    }
    return false;
}

}

0 Answers0