Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Issue with calendar project

  1. #1
    Junior Member phlegm's Avatar
    Join Date
    Nov 2013
    Location
    Rhode Island
    Posts
    12
    My Mood
    Relaxed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Issue with calendar project

    Hello, I am trying to create a project where if the user inputs certain facts (year, month, day that 1st of month falls on, and whether or not a leap year) about the date, it will print the calendar for that month.

    My calendar prints fine except for it only working when the 1st day starts on sunday. Here is some of my code.
    if(month==1)
      {
        String mth = "January";
        System.out.println("\n\tHere is your calendar for " + mth + " " + yr);
        System.out.println("\nSun \tMon \tTues \tWed \tThurs \tFri \tSat\n=================================================================");
         for(int i = day; i < 31; i++)
        {
     
            for(int x = 0; x < 7; x++)
            {
     
     
                System.out.print(i + "\t");
                i++;
                if(x == 6){
                    i--;}
     
                if(i > 31)
                {
                    break;
                }
     
            }
            System.out.print("\n");
        }
     
      }

    If I input year: 2013
    month: January (example code above)
    day that 1st falls on: Monday
    This is what is printed: 6TtHWYce3X.jpg

    I want there to be a 1 under Monday, and a blank space under Sunday because there is no day for the month there. Please don't give me any free answers, but please throw me a bone and give some suggestions. Thanks!
    - phlegm


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Issue with calendar project

    My calendar prints fine except for it only working when the 1st day starts on sunday.
    One of the best belly laughs I've had in a long time.

    I think you know what to do: If the first day of the month is not a Sunday, space over to the appropriate location before printing "1". You can rephrase the statement into a codable algorithm:

    Determine the number of the first day of the week (Sun = 0, Sat = 6)
    Print x * ( numberOfFirstDayOfWeek ) blank spaces
    Print 1 and the rest of the dates in first line
    Continue dates on next line . . .

    Good luck, and thanks for the laugh!

  3. #3
    Junior Member phlegm's Avatar
    Join Date
    Nov 2013
    Location
    Rhode Island
    Posts
    12
    My Mood
    Relaxed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Issue with calendar project

    Glad I made you laugh, and sorry for the late reply, but I'm not quite sure how I would execute that. I've been trying different methods for days and none of them work.
    - phlegm

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Issue with calendar project

    Show what you've tried. It's just spaces * the number of days to skip. Show how you've tried to code that.

  5. #5
    Junior Member phlegm's Avatar
    Join Date
    Nov 2013
    Location
    Rhode Island
    Posts
    12
    My Mood
    Relaxed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Issue with calendar project

    I created 7 integer variables for the days of the week from 0 - 7. I then tried to make it so

    if(day==2) //this is a monday
    {
    I don't know what to do here to multiply 1 and ("\t"). I need a "\t" in the space under Sunday, and also I need there to be no hanging numbers.
    }
    This is my current code:
      if(month==1)
      {
        String mth = "January";
        System.out.println("\n\tHere is your calendar for " + mth + " " + yr);
        System.out.println("\nSun \tMon \tTues \tWed \tThurs \tFri \tSat\n=================================================================");
     
     
        for(int i = 1; i < 31; i++)
        {
            for(int x = 0; x < 7; x++)
            {
     
     
             {
                System.out.print(i + "\t");
                i++;
               //if(x == 6){i--;}
               if(day==2 && x==6){i--;}
               if(day==3 && x==7){i--;}
               if(day==4 && x==8){i--;}
               if(day==5 && x==9){i--;}
               if(day==6 && x==10){i--;}
               if(day==7 && x==11){i--;}
     
                if(i > 31)
                {
                    break;
                }
             }
     
            }
            System.out.print("\n");
        }
     
      }

    But I am still not sure how to accomplish what you are wanting me to do.
    - phlegm

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Issue with calendar project

    I'm not wanting you to do anything. I'm suggesting that you print the appropriate number of spaces before you begin printing the dates. For example, if the first day of the week happens to be a Wednesday, then I would add an appropriate number of spaces before printing the '1' for Wednesday, something like:
    // shows how to skip spaces before printing a date
    public class TestClass4
    {
        // a simple main() method that prints spaces and a date
        public static void main( String[] args )
        {
            StringBuilder weekDates = new StringBuilder();
     
            // print a header for the month
            System.out.println( "  S  M  T  W  T  F  S  ");
     
            // add extra spaces to start the first week of the month,
            // start with 3 days for each day after Sunday
            // Sun = 0, Mon = 1, Tue = 2, Wed = 3, . . . 
            // assuming my first month begins on Wed, firsDayOfMonth = 3
            int firstDayOfMonth = 3;
     
            // then set up a loop to build the string for the first week
            for ( int i = 0 ; i < firstDayOfMonth ; i++ )
            {
                // add 3 spaces for each empty day of first week
                weekDates.append( "   " );
            }
     
            // then add the dates for the rest of the first week
            for ( int i = 1 ; i <= ( 7 - firstDayOfMonth ) ; i ++ )
            {
                weekDates.append( "  " + i );
            }
     
            System.out.println( weekDates );
     
        } // end method main()
     
    } // end class TestClass4
    I used a StringBuilder object which I recommend you use, but you don't have to.

Similar Threads

  1. [SOLVED] java calendar
    By wdh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2012, 03:37 PM
  2. [SOLVED] Calendar Issue
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 11th, 2010, 01:19 PM
  3. Calendar help
    By moonieass13 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 31st, 2009, 12:43 AM
  4. calendar
    By subhvi in forum AWT / Java Swing
    Replies: 2
    Last Post: September 29th, 2009, 11:02 PM
  5. which calendar to use?
    By rptech in forum AWT / Java Swing
    Replies: 6
    Last Post: August 30th, 2009, 03:18 PM