Display Calendars in Console Program
I am creating a program that prompts the user to enter a year and the first day of the month, Sun=0, Mon=1, etc . . . then shows that years calendar with the correct number date placement, but I am having severe trouble getting the actual numbered dates up... Honestly I am just not quite sure how to do it. The calendars need to be displayed in the console. Any help is appreciated... I'm just lost.
Here's what I have so far:
Code java:
import javax.swing.JOptionPane;
public class test {
public static void main(String[] args) {
// Prompt the user to enter input
String yearString = JOptionPane.showInputDialog("Enter a year:");
int year = Integer.parseInt(yearString);
String firstDayString = JOptionPane.showInputDialog("Enter the first day of the year:");
int firstDay = Integer.parseInt(firstDayString);
int numberOfDaysInMonth = 0;
// Display calendar for each month
for (int month = 1; month <= 12; month++) {
// Display Calendar title
switch (month) {
case 1: System.out.printf(" January\n");
System.out.printf("=======================================================\n");
System.out.printf("Sun Mon Tues Wed Thurs Fri Sat\n");
numberOfDaysInMonth = 31;
break;
case 2: System.out.printf(" February\n");
System.out.printf("=======================================================\n");
System.out.printf("Sun Mon Tues Wed Thurs Fri Sat\n");
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
numberOfDaysInMonth = 29;
else
numberOfDaysInMonth = 28;
break;
case 3: System.out.printf(" March\n");
System.out.printf("=======================================================\n");
System.out.printf("Sun Mon Tues Wed Thurs Fri Sat\n");
numberOfDaysInMonth = 31;
break;
case 4: System.out.printf(" April\n");
System.out.printf("=======================================================\n");
System.out.printf("Sun Mon Tues Wed Thurs Fri Sat\n");
numberOfDaysInMonth = 30;
break;
case 5: System.out.printf(" May\n");
System.out.printf("=======================================================\n");
System.out.printf("Sun Mon Tues Wed Thurs Fri Sat\n");
numberOfDaysInMonth = 31;
break;
case 6: System.out.printf(" June\n");
System.out.printf("=======================================================\n");
System.out.printf("Sun Mon Tues Wed Thurs Fri Sat\n");
numberOfDaysInMonth = 30;
break;
case 7: System.out.printf(" July\n");
System.out.printf("=======================================================\n");
System.out.printf("Sun Mon Tues Wed Thurs Fri Sat\n");
numberOfDaysInMonth = 31;
break;
case 8: System.out.printf(" August\n");
System.out.printf("=======================================================\n");
System.out.printf("Sun Mon Tues Wed Thurs Fri Sat\n");
numberOfDaysInMonth = 31;
break;
case 9: System.out.printf(" September\n");
System.out.printf("=======================================================\n");
System.out.printf("Sun Mon Tues Wed Thurs Fri Sat\n");
numberOfDaysInMonth = 30;
break;
case 10: System.out.printf(" October\n");
System.out.printf("=======================================================\n");
System.out.printf("Sun Mon Tues Wed Thurs Fri Sat\n");
numberOfDaysInMonth = 31;
break;
case 11: System.out.printf(" November\n");
System.out.printf("=======================================================\n");
System.out.printf("Sun Mon Tues Wed Thurs Fri Sat\n");
numberOfDaysInMonth = 30;
break;
case 12: System.out.printf(" December\n");
System.out.printf("=======================================================\n");
System.out.printf("Sun Mon Tues Wed Thurs Fri Sat\n");
numberOfDaysInMonth = 31;
break;
}
// switch (firstDay) {
//
// case 0: System.out.println("Sunday"); break;
// case 1: System.out.println("Monday"); break;
// case 2: System.out.println("Tuesday"); break;
// case 3: System.out.println("Wednesday"); break;
// case 4: System.out.println("Thursday"); break;
// case 5: System.out.println("Friday"); break;
// case 6: System.out.println("Saturday"); break;
// }
// // Get the start day for the next month
firstDay = (firstDay + numberOfDaysInMonth) % 7;
}
}
}
I just made this which displays the right format for one month... but if i kept this method up it would take up so many lines of code. My professor said that it must be done in under 150 lines.
Code java:
public class NewClass {
public static void main(String[] args) {
for(int num1 = 1; num1 <= 7; num1++){
System.out.print(num1 + " ");
}
System.out.println("\n");
for(int num2 = 8; num2 <= 14; num2++){
System.out.print(num2+" ");
}
System.out.println("\n");
for(int num3 = 15; num3 <= 21; num3++){
System.out.print(num3+" ");
}
System.out.println("\n");
for(int num4 = 22; num4 <= 28; num4++){
System.out.print(num4+" ");
}
System.out.println("\n");
for(int num5 = 29; num5 <= 31; num5++){
System.out.print(num5+" ");
}
}
}