Adam7397
Adam7397

Reputation: 57

Java Basic Calendar Printing

I am working on an assignment for my computer science 1 course and am stuck on a certain part. The code is made so that the user will input the year, and then the day of the week(using 0 to 6) to start the calendar. I am going off of my instructors skeleton for the code. My current problem is that I can't get the code to print a new line after 7 days. Thanks ahead of time!

import java.util.Scanner;
public class Test {

        /**
         * Determines if an input is a leap year
         * 
         * @param year year in question
         * @return true if a leap year
         */
    public static boolean isLeapYear(int year) {
        if((year % 4 == 0) || (year % 100 == 0)) return true;
        return false;
        }

        /**
         * Outputs a month to the console
         * 
         * @param month title
         * @param startDay 0=Sunday ... 6=Saturday
         * @param numDays number of days in the month
         * @return day of the week of the last day of the month
         */
        public static int printMonth(String month, int startDay, int numDays){
            System.out.println(month);
            int dayOfWeek = 10;

            for(int i = 0; i < startDay; i++){
                System.out.print("  ");
            }

            for(int i = 1; i < numDays; i++){
                System.out.printf("%2d ", i);
                if((i + startDay) % 7 == 0);{
                    System.out.print("");
                }
            }
            //Your code goes here
            System.out.println("Print: " + month);
            System.out.println("");

            return dayOfWeek;
        }

        /**
         * Program execution point:
         * input year, day of the week (0-6) of january 1
         * output calendar for that year
         * 
         * @param args command-line arguments (ignored)
         */
        public static void main(String[] args) {
            @SuppressWarnings("resource")
            final Scanner input = new Scanner(System.in);

            System.out.print("Enter the year: ");
            final int year = input.nextInt();

            System.out.print("Enter the day of the week of January 1st (0=Sunday, 1=Monday, ... 6=Saturday): ");
            final int firstDay = input.nextInt();

            if (year<=0) {
                System.out.println("The year must be positive!");
                System.exit(0);
            }

            if (firstDay<0 || firstDay>6) {
                System.out.println("The day of January 1st must be between 0 and 6!");
                System.exit(0);
            }

            final int numFebDays;
            if (isLeapYear(year)) {
                numFebDays = 29;
            } else {
                numFebDays = 28;
            }

            int lastDayOfWeek;
            lastDayOfWeek = printMonth("January", firstDay, 31);
            lastDayOfWeek = printMonth("February", lastDayOfWeek, numFebDays);
            lastDayOfWeek = printMonth("March", lastDayOfWeek, 31);
            lastDayOfWeek = printMonth("April", lastDayOfWeek, 30);
            lastDayOfWeek = printMonth("May", lastDayOfWeek, 31);
            lastDayOfWeek = printMonth("June", lastDayOfWeek, 30);
            lastDayOfWeek = printMonth("July", lastDayOfWeek, 31);
            lastDayOfWeek = printMonth("August", lastDayOfWeek, 31);
            lastDayOfWeek = printMonth("September", lastDayOfWeek, 30);
            lastDayOfWeek = printMonth("October", lastDayOfWeek, 31);
            lastDayOfWeek = printMonth("November", lastDayOfWeek, 30);
            lastDayOfWeek = printMonth("December", lastDayOfWeek, 31);

        }
}

Upvotes: 1

Views: 808

Answers (1)

Herb
Herb

Reputation: 650

There are several problems with this program. Two of them are mentioned in the comments, changing the System.out.print("") to System.out.println(""), and removing the ; at the end of the for statement.

In addition, you're not changing the value of dayOfWeek. It always returns 10, which is why it's printing so far to the right. It should be

int dayOfWeek = (startDay + numDays) % 7;

I also noticed the formula for calculating a leap year is slightly incorrect. A year is a leap year, if it is divisible by 4, unless it's also deliverable by 100, except it is a leap year, if it's divisible by 400. Here's a function that works:

public static boolean isLeapYear(int year) {
    return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) ;
}

Another error I saw was that the last day of the month was not printing. That is fixed by changing this section of the for loop: i <= numDays;

Upvotes: 1

Related Questions