Results 1 to 9 of 9
Thread: Date highlight
- 11-24-2010, 06:52 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
Date highlight
Hi guys, How are you all. I am a beginner java programmer, and i am trying to get my program to highlight the date which has been given in the month by a command line argument. For instance, there are 3 command line arguments; the first is the start day of the month, second is the month, and the third is the date which we want to highlight in the month. I am guessing a for loop is required, but i cannot make it work. Here is the code:
Any suggestions? I'm so stuck :(Java 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
Kind regards
Shyam
- 11-24-2010, 08:46 PM #2
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
Fixed
Your main problem was that you used the highlight date as the "for" terminator. I fixed that, and added an array for finding how many days are in the month. The only argument you need now is the month number, because all months start with day one, right? Here is your (edited) code:
Java 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])); } // main // Print the calendar for the month. private static void printMonth(int lastDateInMonth) { // Set Number of Days in Each Month int[] monthDay = new int[12]; monthDay[0]=31; monthDay[1]=28; monthDay[2]=31; monthDay[3]=30; monthDay[4]=31; monthDay[5]=30; monthDay[6]=31; monthDay[7]=31; monthDay[8]=30; monthDay[9]=31; monthDay[10]=30; monthDay[11]=31; // Find out how many days are in this month int x = 0; int dateEnd=0; while (x<12) { if (lastDateInMonth-1 == x) { dateEnd=monthDay[x]; } x=x+1; } // Month always starts with one int monthStartDay = 1; // 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 <= dateEnd || 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 > dateEnd) 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
- 11-25-2010, 12:17 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
I may have not made myself clear. Sorry.
Basically, I have created a program which outputs a calendar with 2 given command line arguments; the first being the day of the week the month starts from, an integer (i.e. 1 to 7); the second being the end date of the month i.e. 31.
I have no problems so far, but i am trying to get the program to highlight the date using a third command line argument, which is the date to be highlighted.
I am trying to highlight the date with >< on each side. (e.g. >25<)
Hope this makes more sense. Sorry if you didnt understand earlier.
Kind regards
Shyam :)
- 11-25-2010, 01:35 PM #4
Sorry..
Still not clear with your requirement.Please clearly tell me by giving some sample for what inputs you are giving and what needs to be highlighetd.
Your code is not having any logic for highlighting...Ramya:cool:
- 11-25-2010, 06:24 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
You can see my code previously, I just want some help with some code so that it highlights the date that is typed as the 3rd command lijne argument. For example if i type: java CalendarHighlight 3 28 9, it should produce a calendar starting from the 3rd day of the week i.e. tuesday, and the last date should be 28 and it should highlight the 9th.
Does this make anything better?
Sorry if it doesnt. That's the best i can explain it.
Kind regards
Shyam
- 11-26-2010, 08:24 AM #6
You are telling 3 arguments ..but your program is accepting only one right?
Ramya:cool:
- 11-26-2010, 10:19 AM #7
Just check the file attached whether u want the output like this.
Ramya:cool:
- 11-26-2010, 10:41 AM #8
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
Yes That's exactly how i want it! How did you do that!!!
Regards
Shyam
- 11-26-2010, 02:19 PM #9
//Look for my changes with the keyword "Ramya"
.
// 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
{
private static int highlightDate = 0; //ramya
public static void main(String [] args)
{
printMonth(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]/*ramya*/));
} // main
// Print the calendar for the month.
private static void printMonth(int monthStartDay,
int lastDateInMonth, int dateToHighlight/*ramya*/)
{
// Keep track of which day 1 to 7 is the next day to be printed
// out.
highlightDate = dateToHighlight; //ramya
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
{
//ramya - start
if (nextDateToPrint == highlightDate) {
System.out.print(">");
}
//ramya - end
printDate(nextDateToPrint);
//ramya - start
if (nextDateToPrint == highlightDate) {
System.out.print("<");
}
//ramya - end
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 CalendarHighlightRamya:cool:
Similar Threads
-
Highlight rows of a jtable
By simmi in forum AWT / SwingReplies: 5Last Post: 08-20-2009, 01:13 PM -
How to Highlight a String in JTextArea
By ramvaidhya in forum AWT / SwingReplies: 6Last Post: 12-31-2008, 07:23 AM -
Compare date input to database with current date
By hungleon88 in forum Advanced JavaReplies: 2Last Post: 11-25-2008, 08:10 AM -
Creating a Gregorian Calendar using a Date object gives date - 1
By prachi_goliwadekar in forum New To JavaReplies: 1Last Post: 05-08-2008, 08:32 PM -
GNU Source-highlight 2.7
By levent in forum Java SoftwareReplies: 0Last Post: 06-12-2007, 08:39 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks