Ok, I am working on a program that lets a user enter a year which they want a calender for. The program figures out if the year is a leap year (completed), what day January 1st falls on so that the calender has a starting point (completed, see new code below), and it also assigns each month its number of days (completed).
(see images at the bottom for the code completed thus far. Arrows indicate println's I am using for testing purposes....disregard those...and some code I have changed from the time I scanned the images....which was days ago.)

So the question is: How would I use System.out.print/System.out.println's to print out the calender as shown:

January
S M T W T F S
---------------
(numbers)
WITH PROPER FOMATTING SO THAT THE NUMBERS LINE UP WITH THE DAYS!

.
.
.

December
S M T W T F S
---------------
(numbers)

DayOfTheWeek code:

public int DayOfTheWeek (int yr)
{
   int a, y, m, day;

   a = (14 - 1) / 12;
   y = yr - a;
   m = 1 + (12 * a) - 2;

   day = (1 + y + (y/4) - (y/100) + (y/400) + ((31*m)/12)) % 7;
   return day; //The return "day" will equal 0 for Sunday, 1 for Monday, etc.
}

Also, I tried getting the System.out.print/println's to print out the calender, but I'm getting nowhere.
The way I did the first day was to use 7 IF's that check to see which day Jan 1 is.
Ex.) 0 = Sunday, 1 = Monday ...etc..

Then, under each if is: System.out.print(" ");

Ex.) If the first day is a tuesday then the System.out.print looks like this:

System.out.print(" (5 blank spaces) " + day);
//day is the value of the for loop, which would be 1 the first time through.

But of course this system of IF's would only execute if the calender is only printing the 1st line. I'm stuck on the rest of the printing.....HELP!

Thanks to all who help :mrgreen:

Attachments cal1.jpg 107.48 KB cal2.jpg 117.81 KB

Please help me :sad:. I'm at wits end trying to figure out how to print this calender!!!
Here is the method for printing I came up with:

public void calPrint (int year, int leapyear) {
    Month m = new Month();
    int start = DayOfTheWeek(year);
    int count = 0;
    int range = 0;
    String[] monthName= new monthName ["January","February","March","April","May","June","July","August","September","October","November","December"];
    
    System.out.println("---------------------------");
    System.out.println("    " + year);
    System.out.println(" ");
    
    for (int printcal = 1; printcal <= 12; printcal++){
        System.out.println("   " + monthName[printcal]);
        System.out.println(" S  M  T  W  T  F  S");
        System.out.println("____________________");
        
        // Numbers are printed here:
        for (int day = 1; day <= (m.getMonth(printcal, leapyear)); day++){
            if (printcal == 1)
            {
                while (count != start)
                {
                // prints the blank spaces in front of the first day (may need work)
                    System.out.print(" "); 
                    count ++;
                }
            }
           
            // The following needs work: 
            range = day + 1; 
            
            if (range == 7){
                System.out.print("\n");
                range = 0;
            }
            else
            {
                System.out.print(day + "  ");
                range++;
            }
         
        }
    System.out.println("\n\n");        
    }
}

It prints out...but not well. Some dates are out of line.....and some dates don't start on the right day. Also, some months have an extra day...or one less day...weird. Any help is appreciated! I'd like to finish this program in the next day or two.
Count your help as my x-mas present :mrgreen: .

This article has been dead for over six months. Start a new discussion instead.