5 Replies - 16771 Views - Last Post: 21 February 2010 - 02:00 PM Rate Topic: -----

#1 Charlie IronGleet  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Displaying Calendar for Each Month of Given Year/Day

Posted 15 February 2009 - 07:13 AM

Hello,

I'm working on a program that prompts the user to enter the year and first day of the year and displays the calendar table for that year on the console. For example, if the user entered the year 2005, and 6 for Saturday, Juanuary,1, 2005, the program should then display the calendar for each month of the year.

I have written the program and solved the problem for the most part. Only, I'm having terrific difficulty getting the output to properly format. I'm *almost* there. I'll post my program below and then in the following post I'll show you my output. If someone could show me how to tweak this to get it to display right -- I'd be very grateful. Thanks!!!
import java.util.Scanner;

public class DisplayCalendar {
	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;
			switch(month){
				case 1: firstDay=getFirstDay;
				break;
				case 2: firstDay=getFirstDay+3;
				break;
				case 3: firstDay=getFirstDay+3;
				break;
				case 4: firstDay=getFirstDay+6;
				break;
				case 5: firstDay = getFirstDay + 8;
				break;
				case 6: firstDay = getFirstDay + 11;
				break;
				case 7: firstDay = getFirstDay + 13;
				break;
				case 8: firstDay = getFirstDay + 16;
				break;
				case 9: firstDay = getFirstDay + 19;
				break;
				case 10: firstDay = getFirstDay + 21;
				break;
				case 11: firstDay = getFirstDay + 24;
				break;
				case 12: firstDay = getFirstDay + 26;
				break;
			}
			if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
				switch(month){
				case 1: firstDay=getFirstDay;
					break;
					case 2: firstDay=getFirstDay+3;
					break;
					case 3: firstDay=getFirstDay+4;
					break;
					case 4: firstDay=getFirstDay+7;
					break;
					case 5: firstDay = getFirstDay + 9;
					break;
					case 6: firstDay = getFirstDay + 12;
					break;
					case 7: firstDay = getFirstDay + 14;
					break;
					case 8: firstDay = getFirstDay + 17;
					break;
					case 9: firstDay = getFirstDay + 20;
					break;
					case 10: firstDay = getFirstDay + 22;
					break;
					case 11: firstDay = getFirstDay + 25;
					break;
					case 12: firstDay = getFirstDay + 27;
					break;
				}

}
			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();
		}
	}
}




___________________________________________
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


See, my spacing is all off. Again, any help would be wonderful!!

This post has been edited by Charlie IronGleet: 15 February 2009 - 07:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Displaying Calendar for Each Month of Given Year/Day

#2 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Displaying Calendar for Each Month of Given Year/Day

Posted 15 February 2009 - 07:23 AM

hello, Can you please post using code tags so that we could read your code more clearly? :code: thanks ;)
Was This Post Helpful? 0
  • +
  • -

#3 Charlie IronGleet  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Displaying Calendar for Each Month of Given Year/Day

Posted 15 February 2009 - 07:46 AM

View Postayman_mastermind, on 15 Feb, 2009 - 06:23 AM, said:

hello, Can you please post using code tags so that we could read your code more clearly? :code: thanks ;)

Thanks! That was my next question, I was trying to figure out how to do just that :>

Anyways, ideas?
Was This Post Helpful? 0
  • +
  • -

#4 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Displaying Calendar for Each Month of Given Year/Day

Posted 15 February 2009 - 08:00 AM

you mean you want to make sort of columns for all the numbers under monday, tuesday, etc.. to be organized under each other?
Was This Post Helpful? 0
  • +
  • -

#5 Charlie IronGleet  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Displaying Calendar for Each Month of Given Year/Day

Posted 15 February 2009 - 08:14 AM

View Postayman_mastermind, on 15 Feb, 2009 - 07:00 AM, said:

you mean you want to make sort of columns for all the numbers under monday, tuesday, etc.. to be organized under each other?


Wow -- it looks like the forum didn't display my output in the form I had pasted it up.

for some reason, it won't display properly here online. Every time I try to copy and paste or even type in the table as its displaying in my console the damn forum program reformats it!!

Heres the gist: my first row of dates is displayed way off center to the right.

SO I have my Sun Mon Tue Weds......
and then say a one was supposed to be under the Sun -- instead its way off to the right of the table.

So yeah -- I want it to display as you describe above in your question to me.

Thank you,
IronGleet

This post has been edited by Charlie IronGleet: 15 February 2009 - 08:15 AM

Was This Post Helpful? 0
  • +
  • -

#6 parrygill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-February 10

Re: Displaying Calendar for Each Month of Given Year/Day

Posted 21 February 2010 - 02:00 PM

Try This

/*
	ID:
	Name: Parry Gill
	Date: February 21, 2010

	Description: Display Calender
*/
import java.util.*;
public class Four29
{
	public static void main(String[] args) 
	{	
		Scanner in = new Scanner(System.in);
		
		System.out.print("Enter year: ");
		int yr = in.nextInt();

		System.out.print("\nEnter first day, between 0 to 6, 0 for Sunday and so on..: ");
		int day = in.nextInt();

		for (int m=1; m<=12; m++)
		{
			String monthN="";
			int numD=0;
			switch (m)
			{
			case 1:
				monthN="January ";
				numD=31;
				break;
			case 2:
				monthN="February ";
				if ((yr%4 == 0 && yr%100 != 0) || yr%400 == 0 )
					numD=29;
				else numD=28;
				break;
			case 3:
				monthN="March ";
				numD=31;
				break;
			case 4:
				monthN="April ";
				numD=30;
				break;
			case 5:
				monthN="May ";
				numD=31;
				break;
			case 6:
				monthN="June ";
				numD=30;
				break;
			case 7:
				monthN="July ";
				numD=31;
				break;
			case 8:
				monthN="August ";
				numD=31;
				break;
			case 9:
				monthN="September ";
				numD=30;
				break;
			case 10:
				monthN="October ";
				numD=31;
				break;
			case 11:
				monthN="November ";
				numD=30;
				break;
			case 12:
				monthN="December ";
				numD=31;
				break;
			}

			System.out.println("\n             " + monthN + yr);
			System.out.println("_______________________________________");
			System.out.println("Sun   Mon   Tue   Wed   Thu   Fri   Sat");
			for (int sp=1; sp<=day; sp++)
					System.out.print("      ");
			for (int p=1; p<=numD; p++)
			{	
				if (day%7==0 && day!=0)
					System.out.println();
				System.out.printf("%3d   ", p);
				day+=1;
			}
			day%=7;
			
			System.out.print("\n\n");
		}
	} // end of main method
} // end of class


This post has been edited by parrygill: 21 February 2010 - 06:35 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1