4 Replies - 17196 Views - Last Post: 25 June 2007 - 07:26 PM Rate Topic: -----

#1 sonzai  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 31-October 06

Print Calendar

Posted 20 June 2007 - 12:59 PM

Ok, so this is the question:
I gotta print all months of the year in their calendars given the year and the first day of the month.
I don't understand how to put this in a table.
Of course I got the headings ready...but that's as far as I got...
I just want to know how to put the days in their specific day of the week in calendar form with only the first day of the year...
It's been 2 hours since I started and I still got nothing

this is as far as I can go, but sth is still so wrong...
it's pretty messed up...
import java.util.Scanner;

public class calendar {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("Enter the year: ");
		int year = scanner.nextInteger();
		
		System.out.print("Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): ");
		int firstDay = scanner.nextInteger();
		
		for ( int month = 1; month <= 12; month++ ){
			System.out.println("	   " + month + " " + year);
			System.out.println("---------------------------------");
			System.out.println("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
		}
		
		String monthName = " ";
		switch (month) {
			case 1: monthName = "January";
					 days = 31;
					 break;
			case 2: monthName = "February";
					 if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
						 days = 29;
						 } else {
							 days = 28;
							 }
					 break;
			case 3: monthName = "March";
					 days = 31;
					 break;
			case 4: monthName = "April";
					 days = 30;
					 break;
			case 5: monthName = "May";
					 days = 31;
					 break;
			case 6: monthName = "June";
					 days = 30;
					 break;
			case 7: monthName = "July";
					 days = 31;
					 break;
			case 8: monthName = "August";
					 days = 31;
					 break;
			case 9: monthName = "September";
					 days = 30;
					 break;
			case 10: monthName = "October";
					 days = 31;
					 break;
			case 11: monthName = "November";
					 days = 30;
					 break;
			case 12: monthName = "December";
					 days = 31;
					 break;
			default: JOptionPane.showMessageDialog(null,"Error: this month does not exist"); System.exit(0);
					 break;
			}
	}
}


thank you in advance...

Is This A Good Question/Topic? 0
  • +

Replies To: Print Calendar

#2 sonzai  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 31-October 06

Re: Print Calendar

Posted 24 June 2007 - 01:33 PM

ok...since no one replied...
I tried it myself again
I really need help~ ><
What do I need to change so that the first day of the next month follows the last day of the first month?
What am I doing wrong!

import java.util.Scanner;

public class calendar {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("Enter the year: ");
		int year = scanner.nextInt();
		
		System.out.print("Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): ");
		int firstDay = scanner.nextInt();
		
		for ( int month = 1; month <= 12; month++ ){
			int days = 0;
			String monthName = " ";
			switch (month) {
				case 1: monthName = "January";
					days = 31;
				break;
				case 2: monthName = "February";
					if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
						days = 29;
						} else {
							days = 28;
							}
				break;
				case 3: monthName = "March";
					days = 31;
				break;
				case 4: monthName = "April";
					days = 30;
				break;
				case 5: monthName = "May";
					days = 31;
				break;
				case 6: monthName = "June";
					days = 30;
				break;
				case 7: monthName = "July";
					days = 31;
				break;
				case 8: monthName = "August";
					days = 31;
				break;
				case 9: monthName = "September";
					days = 30;
				break;
				case 10: monthName = "October";
					days = 31;
				break;
				case 11: monthName = "November";
					days = 30;
				break;
				case 12: monthName = "December";
					days = 31;
				break;
				default: System.out.print("Error: this month does not exist"); System.exit(0);
				break;
			}
			
			System.out.println("			" + monthName + " " + year);
			System.out.println("-----------------------------------");
			System.out.println("  Sun  Mon  Tue  Wed  Thu  Fri  Sat");
						
			int i = 0;
			for (; i < firstDay; i++ )
				System.out.print("	 ");
			
			for ( i = 1; i <= days; i++ ) {
				if (i < 10)
					System.out.print("	" + i );
				else
					System.out.print("   " + i );
				
				if ((i + firstDay ) % 7 == 0 )
					System.out.println();
			}
			System.out.println();
		}
	}
}


I want it to display sth like this:
(sorry about bad alignment...)

January 2005
___________________________________________
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

February 2005
____________________________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28

March 2005
____________________________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

......
Was This Post Helpful? 0
  • +
  • -

#3 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Print Calendar

Posted 24 June 2007 - 02:34 PM

Hi, use another switch statement like you have with the months.
For each month try using the code
  int i = 0;
			for (; i < firstDay; i++ )
				System.out.print("	 ");
			
			for ( i = 1; i <= days; i++ ) {
				if (i < 10)
					System.out.print("	" + i );
				else
					System.out.print("   " + i );
				
				if ((i + firstDay ) % 7 == 0 )
					System.out.println();
			}
			System.out.println();
		}
you have written.

Now this very code should be used only for the first month, for the second month use similar code but you should use a system in which you'll calculate the place where you'll put the begining day. The calculations should be based on the number of days of the previous month and the position of the first day. I haven't try this it's just an idea: if the days of the month are 31 move for 4 places in front
e.g.
1-st January is on Mon
Then first of Feb would be on Fri

say
1-st January is on Sat then move 4 positions to Wed
think it through

if the month days are 30 move 3 positions

if 29 move 2
if 28 one... try it it's just an idea

This post has been edited by PennyBoki: 24 June 2007 - 02:36 PM

Was This Post Helpful? 0
  • +
  • -

#4 sonzai  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 31-October 06

Re: Print Calendar

Posted 25 June 2007 - 05:16 PM

ok I finally did it
but how do I change something to a while loop...
instead of doing too many if-else statements...
import java.util.Scanner;

public class calendar {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("Enter the year: ");
		int year = scanner.nextInt();
		
		System.out.print("Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): ");
		int getFirstDay = scanner.nextInt();
		
		for ( int month = 1; month <= 12; month++ ){
			int days = 0;
			String monthName = " ";
			switch (month) {
				case 1: monthName = "January";
					days = 31;
				break;
				case 2: monthName = "February";
					if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
						days = 29;
						} else {
							days = 28;
							}
				break;
				case 3: monthName = "March";
					days = 31;
				break;
				case 4: monthName = "April";
					days = 30;
				break;
				case 5: monthName = "May";
					days = 31;
				break;
				case 6: monthName = "June";
					days = 30;
				break;
				case 7: monthName = "July";
					days = 31;
				break;
				case 8: monthName = "August";
					days = 31;
				break;
				case 9: monthName = "September";
					days = 30;
				break;
				case 10: monthName = "October";
					days = 31;
				break;
				case 11: monthName = "November";
					days = 30;
				break;
				case 12: monthName = "December";
					days = 31;
				break;
				default: System.out.print("Error: this month does not exist"); System.exit(0);
				break;
			}
			
			System.out.println("			" + monthName + " " + year);
			System.out.println("-----------------------------------");
			System.out.println("  Sun  Mon  Tue  Wed  Thu  Fri  Sat");
			
			int i = 0;
			int firstDay = 0;
			if ( month == 1 ){
				firstDay = getFirstDay;
			} else if ( month == 2 ){
				firstDay = getFirstDay + 3;
			} else if ( month == 3 ){
				firstDay = getFirstDay + 3;
			} else if ( month == 4 ){
				firstDay = getFirstDay + 6;
			} else if ( month == 5 ){
				firstDay = getFirstDay + 8;
			} else if ( month == 6 ){
				firstDay = getFirstDay + 11;
			} else if ( month == 7 ){
				firstDay = getFirstDay + 13;
			} else if ( month == 8 ){
				firstDay = getFirstDay + 16;
			} else if ( month == 9 ){
				firstDay = getFirstDay + 19;
			} else if ( month == 10 ){
				firstDay = getFirstDay + 21;
			} else if ( month == 11 ){
				firstDay = getFirstDay + 24;
			} else if ( month == 12 ){
				firstDay = getFirstDay + 26;
			}
			
			if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
				if ( month == 1 ){
					firstDay = getFirstDay;
				} else if ( month == 2 ){
					firstDay = getFirstDay + 3;
				} else if ( month == 3 ){
					firstDay = getFirstDay + 4;
				} else if ( month == 4 ){
					firstDay = getFirstDay + 7;
				} else if ( month == 5 ){
					firstDay = getFirstDay + 9;
				} else if ( month == 6 ){
					firstDay = getFirstDay + 12;
				} else if ( month == 7 ){
					firstDay = getFirstDay + 14;
				} else if ( month == 8 ){
					firstDay = getFirstDay + 17;
				} else if ( month == 9 ){
					firstDay = getFirstDay + 20;
				} else if ( month == 10 ){
					firstDay = getFirstDay + 22;
				} else if ( month == 11 ){
					firstDay = getFirstDay + 25;
				} else if ( month == 12 ){
					firstDay = getFirstDay + 27;
				}
			}
				
			int dayOfWeek = 0;
			if ( (firstDay % 7 ) >= 0 ){
				if ( (firstDay % 7 ) == 0 ){
					dayOfWeek = 0;
				} else if ( (firstDay % 7 ) == 1 ){
					dayOfWeek = 1;
					System.out.print("\t " );
				} else if ( (firstDay % 7 ) == 2 ){
					dayOfWeek = 2;
					System.out.print("\t\t  " );
				} else if ( (firstDay % 7 ) == 3 ){
					dayOfWeek = 3;
					System.out.print("\t\t\t   " );
				} else if ( (firstDay % 7 )  == 4 ){
					dayOfWeek = 4;
					System.out.print("\t\t\t\t\t" );
				} else if ( (firstDay % 7 ) == 5 ){
					dayOfWeek = 5;
					System.out.print("\t\t\t\t\t\t " );
				} else if ( (firstDay % 7 ) == 6 ){
					dayOfWeek = 6;
					System.out.print("\t\t\t\t\t\t\t  " );
				}
			}
			
			for ( i = 1; i <= days; i++ ) {
				if (i < 10)
					System.out.print("	" + i );
				else
					System.out.print("   " + i );
				
				if ((i + firstDay ) % 7 == 0 )
					System.out.println();
			}
			System.out.println();
		}
	}
}

Was This Post Helpful? 0
  • +
  • -

#5 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: Print Calendar

Posted 25 June 2007 - 07:26 PM

Why don't you check my tutorial "creating a calendar viewer application" in the tutorial section? It shows you how to make a calendar and switch months, etc...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1