Hi Guys, how are you all. I seem to be having a problem getting my program to highlight the date which is typed as the third command line argument. There are three command line arguments; 1st one is the starting date, second is the month, and the third is the date to highlight.
Here is the code:
// Program to print a calendar for a given month. // First argument is the number of the start day, 1 to 7 // Second argument is the last date in the month e.g. 28. public class CalendarHighlight { public static void main(String [] args) { printMonth(Integer.parseInt(args[0]), Integer.parseInt(args[1])); } // main // Print the calendar for the month. private static void printMonth(int monthStartDay, int lastDateInMonth) { // Keep track of which day 1 to 7 is the next day to be printed // out. int nextDayColumnToUse = monthStartDay; // Keep track of next date to be printed out. int nextDateToPrint = 1; // Print top line of calendar. printMonthLineOfHyphens(); // Print headings i.e. su, mo, tu etc. printDayNames(); // Print out minimum 6 rows to ensure consistent format. int noOfRows = 0; while (nextDateToPrint <= lastDateInMonth || noOfRows < 6) { // Print one row. System.out.print(" | "); for (int dayColumnNo = 1; dayColumnNo <= 7; dayColumnNo++) { // Print a space separator between day columns. if (dayColumnNo > 1) System.out.print(" "); // We either print spaces or date. if (dayColumnNo != nextDayColumnToUse || nextDateToPrint > lastDateInMonth) printDateSpace(); else { printDate(nextDateToPrint); nextDayColumnToUse++; nextDateToPrint++; } // else } // for // Print separator and end the row. System.out.println(" | "); noOfRows++; // The next row. nextDayColumnToUse = 1; } // while // Print bottom line of calendar. printMonthLineOfHyphens(); } // printMonth // Print line of hyphens as wide as table starting with // spaces and ending with spaces so it looks right. private static void printMonthLineOfHyphens() { System.out.print(" "); for (int dayColumnNo = 1; dayColumnNo <= 7; dayColumnNo++) { if (dayColumnNo > 1) System.out.print("---"); printDateHyphens(); } // for System.out.println(" "); } // printMonthLineOfHyphens // Print headings of days in the week. private static void printDayNames() { System.out.print(" | "); for (int dayColumnNo = 1; dayColumnNo <= 7; dayColumnNo++) { if (dayColumnNo > 1) System.out.print(" "); printDayName(dayColumnNo); } // for System.out.println(" | "); } // printDayNames // Print Day names as two characters. private static void printDayName(int dayNo) { // Days in the week are numbered 1 - 7 from sunday so: switch (dayNo) { case 1: System.out.print("Su"); break; case 2: System.out.print("Mo"); break; case 3: System.out.print("Tu"); break; case 4: System.out.print("We"); break; case 5: System.out.print("Th"); break; case 6: System.out.print("Fr"); break; case 7: System.out.print("Sa"); break; } // switch } // printDayName // Print spaces as wide as date between the dates. private static void printDateSpace() { System.out.print(" "); } // printDateSpace // Print hyphens as wide as the date. private static void printDateHyphens() { System.out.print("---"); } // printDateHyphens // Print date using two characters, with leading zero if required. private static void printDate(int date) { System.out.printf("%02d", date); } // printDate } // class CalendarHighlight
Any suggestions guys?
Kind regards
Shyam