Issue with looping forwards

so I'm having an issue with my loops, where the intention is to fill through the whole months before moving to the next line, like so

     January   2000        February   2000        March   2000   
|S  M Tu  W Th  F  S| |S  M Tu  W Th  F  S| |S  M Tu  W Th  F  S|
                   1         1  2  3  4  5            1  2  3  4 
 2  3  4  5  6  7  8   6  7  8  9 10 11 12   5  6  7  8  9 10 11
 9 10 11 12 13 14 15  13 14 15 16 17 18 19  12 13 14 15 16 17 18
16 17 18 19 20 21 22  20 21 22 23 24 25 26  19 20 21 22 23 24 25
23 24 25 26 27 28 29  27                    26 27 28 29 30 31

but my result looks like this -

          January   2000        February   2000        March   2000   
|S  M Tu  W Th  F  S||S  M Tu  W Th  F  S||S  M Tu  W Th  F  S|
                   1        1  2  3  4  5           1  2  3  4 
     April   2000        May   2000        June   2000   
|S  M Tu  W Th  F  S||S  M Tu  W Th  F  S||S  M Tu  W Th  F  S|
                   1     1  2  3  4  5  6              1  2  3 
     July   2000        August   2000        September   2000   
|S  M Tu  W Th  F  S||S  M Tu  W Th  F  S||S  M Tu  W Th  F  S|
                   1        1  2  3  4  5                 1  2 
     October   2000        November   2000        December   2000   
|S  M Tu  W Th  F  S||S  M Tu  W Th  F  S||S  M Tu  W Th  F  S|
 1  2  3  4  5  6  7           1  2  3  4                 1  2 

what can be done or added to make it work fully and go through? I feel like I have a loop issue but I genuinely can't seem to pinpoint it

public static int day(int month, int day, int year) {
        int y = year - (14 - month) / 12;
        int x = y + y / 4 - y / 100 + y / 400;
        int m = month + 12 * ((14 - month) / 12) - 2;
        int d = (day + x + (31 * m) / 12) % 7;
        return d;
    }

    public static boolean isLeapYear(int year) {
        if ((year % 4 == 0) && (year % 100 != 0))
            return true;
        if (year % 400 == 0)
            return true;
        return false;
    }    
public static void main(String[] args) {

    // take in command line argument to determine the month and year
    int month = 0;
    int year = 2000;    
    String[] months = { "", // left empty so that months[1] = "January"
                "January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
                "November", "December" };

        // days[i] = number of days in month i
        int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

        for (month = 1; month < 12;) {

            // check for leap year
            if (month == 2 && isLeapYear(year))
                days[month] = 29;

            // print calendar header
            for (int f = 0; f < 3; f++) {

                System.out.print("     " + months[month] + "   " + year + "   ");
                month++;
            }
            System.out.println();
            for (int f = 0; f < 3; f++) {
                System.out.print("|S  M Tu  W Th  F  S|");
            }
            System.out.println();

            // print the calendar
            month -= 3;
            for (int f = 0; f < 3; f++) {
                int d = day(month, 1, year);
                for (int i = 0; i < d; i++)
                    System.out.print("   ");
                for (int i = 1; i <= days[month]; i++) {

                    System.out.printf("%2d ", i);
                    if (((i + d) % 7 == 0) || (i == days[month])) {
                        if(month<12)
                        month++;
                        break;
                    }

                }

            }
            System.out.println();

        }
728x90

3 Answers Issue with looping forwards

You are missing one loop.

Outer loop goes from leftmost month to righmost month (3 in total). But that will give you just one line.

You have to wrap it with another loop that will go through all the lines.

4 months ago

Your approach is a bit of a headache. I'm not saying it won't work, but nesting loops like that is too complex.

You could define a Month class that'll have header() and getLine(int lineNumber) methods.

You can also have a Quarter class that'll have header() and getLine(int lineNumber) methods. It'll contain three Month objects.

You can then loop through your Quarter objects (4 in a year) and print the header followed by 6 lines.

4 months ago

As Perdi Estaquel saied, you've missed one more loop.

for (month = 1; month < 12;) {
}

only enables days output once a month, and you need to do it like this.

for (month = 1; month < 12;) {
    // month and header output stay unchanged

    for (int calendarLine = 1; calenderLine <= 5; calenderLine++) {
        // your calender output here
    }
}

4 months ago