Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    rebelchild is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Unhappy annualCalendar Java Program

    Working on homework; I'm down to finding to errors; February does not come out to correct number of days; and my year does not display correctly after the month name; while I'm sure it is something simple; I'm simply not seeing the forest for the trees any more; appreciate the help with this.....below is my code and my current output.

    /*

    * Description:Create a calendar when the user is prompted to enter the
    * first day of the year.
    * Lab 2.2 AnnualCalendar.java - Displays a year calendar
    */

    package displayingcalendars;

    import javax.swing.JOptionPane;

    public class DisplayingCalendars {


    public static void main(String[] args) {

    int currentMonth = 1, // month being printed. 1 = Jan.
    currentDay = 1, // day of week. 1 = Sun.
    daysInMonth =1, // number of days in month. Feb = 28 or 29
    dayOfMonth = 1, // calendar day
    year = 1, // The current year (user input)
    firstDay = 1; // Day of week for Jan. 1 (user input)
    // Note: firstDay will be re-used to set the
    // first day of each month.
    String monthName = null;

    // Get input from user: year and first day of year
    String yearInput = JOptionPane.showInputDialog("Please enter calendar year: ");
    String dayInput = JOptionPane.showInputDialog("Please enter first calendar day: ");


    // Use loop to cycle through the months of the year
    for (currentMonth = 1; currentMonth <=12; currentMonth++){
    // Determine current month's name
    switch (currentMonth){
    case 1: monthName = "January";
    break;
    case 2: monthName = "February";
    break;
    case 3: monthName = "March";
    break;
    case 4: monthName = "April";
    break;
    case 5: monthName = "May";
    break;
    case 6: monthName = "June";
    break;
    case 7: monthName = "July";
    break;
    case 8: monthName = "August";
    break;
    case 9: monthName = "September";
    break;
    case 10: monthName = "October";
    break;
    case 11: monthName = "November";
    break;
    case 12: monthName = "December";
    break;

    }

    // Determine number of days in current month
    switch (currentMonth) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
    daysInMonth = 31;
    break;
    case 2: // February. Check for leap year. See Listing 3.2 p. 71
    boolean leapYear =
    (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    break;
    default:
    daysInMonth = 30;
    }

    // Print Month name
    System.out.println("\n\n " + monthName + " " + year);
    System.out.println("-----------------------------");
    System.out.println(" Sun Mon Tue Wed Thu Fri Sat");

    // Print days of month
    // First day may need padding spaces for correct positioning
    if (firstDay > 1){
    System.out.print(" ");
    }
    for (int i = 1; i < firstDay; i++){
    System.out.print(" ");
    }

    currentDay = firstDay;
    dayOfMonth = 1;

    // Cycle through days until we reach the end of the month
    while (dayOfMonth <= daysInMonth){
    if (currentDay > 7){
    // End of week - start new line
    System.out.print("\n ");
    currentDay = 1;
    }
    // Print the day with spaces.
    // Days < 10 need an extra space for correct positioning
    if (dayOfMonth < 10){
    System.out.print(" " + dayOfMonth + " ");
    }
    else {
    System.out.print(" " + dayOfMonth + " ");
    }
    currentDay++;
    dayOfMonth++;
    }
    // End of month. Set first day of next month.
    firstDay = currentDay;
    }
    // End of year. Print a couple lines for good measure...
    System.out.println("\n\n");
    }
    }


    run:


    January 1
    -----------------------------
    Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5 6 7
    8 9 10 11 12 13 14
    15 16 17 18 19 20 21
    22 23 24 25 26 27 28
    29 30 31

    February 1
    -----------------------------
    Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4
    5 6 7 8 9 10 11
    12 13 14 15 16 17 18
    19 20 21 22 23 24 25
    26 27 28 29 30 31

    March 1
    -----------------------------
    Sun Mon Tue Wed Thu Fri Sat
    1
    2 3 4 5 6 7 8
    9 10 11 12 13 14 15
    16 17 18 19 20 21 22
    23 24 25 26 27 28 29
    30 31

    April 1
    -----------------------------
    Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
    6 7 8 9 10 11 12
    13 14 15 16 17 18 19
    20 21 22 23 24 25 26
    27 28 29 30

    May 1
    -----------------------------
    Sun Mon Tue Wed Thu Fri Sat
    1 2 3
    4 5 6 7 8 9 10
    11 12 13 14 15 16 17
    18 19 20 21 22 23 24
    25 26 27 28 29 30 31

    June 1
    -----------------------------
    Sun Mon Tue Wed Thu Fri Sat

    1 2 3 4 5 6 7
    8 9 10 11 12 13 14
    15 16 17 18 19 20 21
    22 23 24 25 26 27 28
    29 30

    July 1
    -----------------------------
    Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
    6 7 8 9 10 11 12
    13 14 15 16 17 18 19
    20 21 22 23 24 25 26
    27 28 29 30 31

    August 1
    -----------------------------
    Sun Mon Tue Wed Thu Fri Sat
    1 2
    3 4 5 6 7 8 9
    10 11 12 13 14 15 16
    17 18 19 20 21 22 23
    24 25 26 27 28 29 30
    31

    September 1
    -----------------------------
    Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5 6
    7 8 9 10 11 12 13
    14 15 16 17 18 19 20
    21 22 23 24 25 26 27
    28 29 30

    October 1
    -----------------------------
    Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4
    5 6 7 8 9 10 11
    12 13 14 15 16 17 18
    19 20 21 22 23 24 25
    26 27 28 29 30 31

    November 1
    -----------------------------
    Sun Mon Tue Wed Thu Fri Sat
    1
    2 3 4 5 6 7 8
    9 10 11 12 13 14 15
    16 17 18 19 20 21 22
    23 24 25 26 27 28 29
    30

    December 1
    -----------------------------
    Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5 6
    7 8 9 10 11 12 13
    14 15 16 17 18 19 20
    21 22 23 24 25 26 27
    28 29 30 31

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    for (currentMonth = 1; currentMonth <=12; currentMonth++){
    // Determine current month's name
    switch (currentMonth){
    case 1: monthName = "January";
    break;
    case 2: monthName = "February";
    break;
    case 3: monthName = "March";
    break;
    case 4: monthName = "April";
    break;
    case 5: monthName = "May";
    break;
    case 6: monthName = "June";
    break;
    case 7: monthName = "July";
    break;
    case 8: monthName = "August";
    break;
    case 9: monthName = "September";
    break;
    case 10: monthName = "October";
    break;
    case 11: monthName = "November";
    break;
    case 12: monthName = "December";
    break;
    
    }
    This is totally pointless. currentMonth starts with a value of 1, enters switch and sets monthName to January. Goes back to loop, inc currentMonth to 2, enters switch and sets monthName to February (overwriting January). Goes back to loop, inc currentMonth to 3, enters switch and sets monthName to March (overwriting February). etc etc. At the end monthName will always be December and currentMonth will always be 12.

  3. #3
    rebelchild is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default while..................

    didn't really address my questions; thank you.......I'm still lost..

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Yes it does. As I said after that for loop/switch currentMonth will always have the value 13. What happens when the code executes the next switch? What value will daysInMonth have?
    Last edited by Junky; 04-07-2011 at 01:01 AM. Reason: Just realised it will be 13 not 12.

  5. #5
    rebelchild is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default thanks i guess

    But sorry what you are telling me doesn't make sense to me this is my second programming class ever, guess I'm just stupid about it. I set my program up the way the instructor explained it. So at least thanks for response even it I don't get it.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    How about you explain what that for loop/switch statement is supposed to do.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    OK, my bad. Since your code has lost formatting I misread it. Apparently all your other code is also inside that for loop.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Your problem with February is that all you do in the case statement is set a boolean. You never change the value of daysInMonth. It will retain the last value it had ie 31 for January.

  9. #9
    rebelchild is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default ok.................

    I thought that was the reason behind using the "break" in the code; I know I didn't get the monthName to display correctly until I used the break; so I thought logically that I needed it again here to get the daysInMonth to display correctly..........

  10. #10
    rebelchild is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default

    case 2: daysInMonth = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));

    is this how I should have coded it? was directed to use a book example so that's exactly what I did...........

  11. #11
    rebelchild is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default

    hmmmmmmm.................guess wasn't correct I get incompatible types error; req "int" found "boolean"

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Of course it doesn't work, daysInMonth is an int and you are trying to assign it a boolean. What uses booleans?

  13. #13
    rebelchild is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default

    not sure I understand your question; isn't it the equation to determine leapYear that makes it boolean; do I need to do a conversion from boolean to int; is that even possible; ???????????????????????????

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Yes what you have will determine a leapYear of not but how do you use it? In plain English tell me what you need to do.

  15. #15
    rebelchild is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default

    with dialog boxes I have to enter a year; say 2010; the day the year starts 4 (for fourth day) my return should be a "calender" simply showing each month; giving month name; the year; at the bottom of my code on my first post I included the run; the two issues I have trying to correctly figure "february" to show 28 days and for a reason I've yet to "see" the year isn't showing the year entered into the dialog box but what the variable year was initialized to.

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    No I mean tell me in plain English how you would know if it is a leap year of not.

    HINT: a very important word is in the above sentence.

  17. #17
    rebelchild is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default

    you determine leapYear by the leapYear equation....................28 or 29 days; idk............. I can "see" where you are going with this I think; somewhere somehow daysInMonth needs to equal 28 or 29 don't have a clue as to how to get it there

  18. #18
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    :headdesk:

    Use an if statement. sheesh!

  19. #19
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If that Boolean expression is true days = one amount, else it's another amount is what Junky was hinting at. You can also use the ternary operator but you may not have covered that yet.

  20. #20
    rebelchild is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default

    I'm sorry; guys; guess if I understood all this I wouldn't have tried to find some help here. I tried the if/else statement got more freaking errors than I knew how to solve. Was told to follow an example; example didn't have the if/else; was told to use switch cases, that doesn't seem to be working for me either; so sorry to have wasted your time; asking rookie questions on something I freaking don't understand and at this point still have very little experience with. I literally have 10 weeks of programming experience and that was with python;

    Again sorry to have wasted your time not understanding.

Page 1 of 2 12 LastLast

Similar Threads

  1. Call one Java Program from another Java Program
    By rajpalparyani in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 04:13 AM
  2. Replies: 4
    Last Post: 07-30-2010, 12:25 PM
  3. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 05:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

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