Need help with calendar

Need help with nested for loops for calendar. Calendar must display e

Page 1 of 1

4 Replies - 1117 Views - Last Post: 22 September 2009 - 04:17 PM Rate Topic: -----

#1 dbfinsfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-August 09

Need help with calendar

Post icon  Posted 21 September 2009 - 10:18 PM

//Program which displays calendar

//Calendars

/**Algorithm
*Read in the year
*Read in the the first day of the year
*Print out calendar to display months and days of the year
*/

//Allows dialog boxes to be created
import javax.swing.JOptionPane;

//Prints to screen
import java.util.Scanner;

//Defines Java class
public class Calendar{


//Main method
public static void main(String[] args){

//Creates a scanner
Scanner scanner = new Scanner(System.in);

//Prompt the user to enter a year
System.out.print("Enter a year: ");

//Converts String getYear to integer
int year = scanner.nextInt();

//Enter start day
System.out.print("Enter first day of the year: ");

//Converts string day to integer
int startDay = scanner.nextInt();

//Days of the week
// System.out.print ("Sun","Mon","Tus","Wed","Thu","Fri","Sat");


//Variable that holds the value for months with 31 days
int numDays = 0;

//For loop which computes number of days in a month
for (int month = 1; month <= 12; month++)

switch (month) {

case 1:
System.out.println( "January " + year);
System.out.println("------------------------------");
System.out.printf("%4s\n","Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat");

daysOfWeek = 0;
numDays = 31;
break;

case 2:
System.out.println( "February " + year);
System.out.println("------------------------------");


if ( ((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0) )
leapYear = 29;
else
leapYear = 28;
break;

case 3:
System.out.println( "March " + year);
System.out.println("------------------------------");
numDays = 31;
break;

case 4:
System.out.println( "April " + year);
System.out.println("------------------------------");
numDays = 30;
break;

case 5:
System.out.println( "May " + year);
System.out.println("------------------------------");
numDays = 31;
break;

case 6:
System.out.println( "June " + year);
System.out.println("------------------------------");
numDays = 30;
break;

case 7:
System.out.println( "July " + year);
System.out.println("------------------------------");
numDays = 31;
break;

case 8:
System.out.println( "August " + year);
System.out.println("------------------------------");
numDays = 31;
break;

case 9:
System.out.println( "September " + year);
System.out.println("------------------------------");
numDays = 30;
break;

case 10:
System.out.println( "October " + year);
System.out.println("------------------------------");
numDays = 31;
break;

case 11:
System.out.println( "November " + year);
System.out.println("------------------------------");
numDays = 30;
break;

case 12:
System.out.println( "December " + year);
System.out.println("------------------------------");
numDays = 31;
break;

default:
System.out.println("Invalid month.");
break;
}

//Variable to compute starting day of the year
//int fdy = (startDay + numDays) % 7;


}
}

This post has been edited by dbfinsfan: 22 September 2009 - 08:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with calendar

#2 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Need help with calendar

Posted 22 September 2009 - 07:32 AM

First of all, don't post your questions on the description field, we can't even read your question completely.
And then, always remember to put code tags around your code.
Now, are you receiving any errors from your code?
Was This Post Helpful? 0
  • +
  • -

#3 dbfinsfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-August 09

Re: Need help with calendar

Post icon  Posted 22 September 2009 - 08:08 AM


//need help with making calendar which shows each month 
//the days in the month and the correct starting day of the year.  
//Must have nested for loops for the positioning of the 0f the days and numbers. 
// Days must be spaced evenly and numbers should be spaced by 4. 
//Program which displays a calendar

//Calendars

/**Algorithm
*Read in the year
*Read in the the first day of the year
*Print out calendar to display months and days of the year
*/

//Allows dialog boxes to be created
import javax.swing.JOptionPane;

//Prints to screen
import java.util.Scanner;

//Defines Java class
public class Calendar{


  //Main method
  public static void main(String[] args){
  
	//Creates a scanner
	 Scanner scanner = new Scanner(System.in);
  
	//Prompt the user to enter a year
	 System.out.print("Enter a year:  ");
	 
	 //Converts String getYear to integer
	 int year = scanner.nextInt();
	 
	 //Enter start day
	 System.out.print("Enter first day of the year:  ");
	 
	 //Converts string day to integer
	 int startDay = scanner.nextInt();
	 
	 //Days of the week
	// System.out.print ("Sun","Mon","Tus","Wed","Thu","Fri","Sat");
	 
	 
	 //Variable that holds the value for months with 31 days
	 int numDays = 0;
	 	 	 
	 //For loop which computes number of days in a month
	 for (int month = 1; month <= 12; month++)
	 
	   switch (month) {
	
			case 1: 
					System.out.println( "January  " + year); 
				System.out.println("------------------------------");
					System.out.printf("%4s\n","Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat");

					daysOfWeek = 0;
			   numDays = 31;
			   break;
					
				case 2:
					System.out.println( "February  " + year); 
				System.out.println("------------------------------");

					  
				if ( ((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0) )
					leapYear = 29;
				else
					leapYear = 28;
				break;
					 
				case 3:
				   System.out.println( "March  " + year); 
				System.out.println("------------------------------");
			   numDays = 31;
			   break;
					
			   case 4:
				   System.out.println( "April  " + year); 
				System.out.println("------------------------------");
			   numDays = 30;
			   break;
					
			   case 5:
				   System.out.println( "May  " + year); 
				System.out.println("------------------------------");
			   numDays = 31;
			   break;
		   
			   case 6:
				   System.out.println( "June  " + year); 
				System.out.println("------------------------------");
			   numDays = 30;
			   break;
					
			   case 7:
				   System.out.println( "July  " + year); 
				System.out.println("------------------------------");
			   numDays = 31;
			   break;
					
				 case 8:
				   System.out.println( "August  " + year); 
				System.out.println("------------------------------");
			   numDays = 31;
			   break;
			
				 case 9:
					System.out.println( "September  " + year); 
				   System.out.println("------------------------------");
				numDays = 30;
				break;
					 
				 case 10:
					System.out.println( "October  " + year); 
				   System.out.println("------------------------------");
				numDays = 31;
				break;
					 
				 case 11:
					System.out.println( "November  " + year); 
				   System.out.println("------------------------------");
				numDays = 30;
				break;
			
				 case 12:
					System.out.println( "December  " + year); 
				   System.out.println("------------------------------");
				numDays = 31;
				break;

			 default:
				System.out.println("Invalid month.");
				break;
		}
		  
	//Variable to compute starting day of the year
	//int fdy = (startDay + numDays) % 7;
	
	
}
}	

		  

[quote]

Was This Post Helpful? 0
  • +
  • -

#4 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Need help with calendar

Posted 22 September 2009 - 09:00 AM

One post was appropriate enough. Please do not double post and expect an answer faster, it just doesn't work...
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Need help with calendar

Posted 22 September 2009 - 04:17 PM

Duplicated topics merged
and please :code:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1