Results 1 to 9 of 9

Thread: Date highlight

  1. #1
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default 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:

    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
    Any suggestions? I'm so stuck :(

    Kind regards

    Shyam

  2. #2
    meta1203 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Thumbs up 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

  3. #3
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    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 :)

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    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:

  5. #5
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    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

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    You are telling 3 arguments ..but your program is accepting only one right?
    Ramya:cool:

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    Just check the file attached whether u want the output like this.
    Attached Thumbnails Attached Thumbnails Date highlight-picture-device-independent-bitmap-1.jpg  
    Ramya:cool:

  8. #8
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    Yes That's exactly how i want it! How did you do that!!!

    Regards

    Shyam

  9. #9
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    //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 CalendarHighlight
    Ramya:cool:

Similar Threads

  1. Highlight rows of a jtable
    By simmi in forum AWT / Swing
    Replies: 5
    Last Post: 08-20-2009, 01:13 PM
  2. How to Highlight a String in JTextArea
    By ramvaidhya in forum AWT / Swing
    Replies: 6
    Last Post: 12-31-2008, 07:23 AM
  3. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 08:10 AM
  4. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 08:32 PM
  5. GNU Source-highlight 2.7
    By levent in forum Java Software
    Replies: 0
    Last Post: 06-12-2007, 08:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •