Results 1 to 4 of 4
  1. #1
    jaydubya is offline Member
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Default Displaying 12 calendar months?

    I am brand new to Java. Just started class last week.
    I am using code from the book we get with the class.
    When I run the program it prints out just the one calendar month I input.
    How can I modify the code to display the rest of the months that follow for 12 calendar months?
    Here is the code.
    Sorry if it looks messy.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package calendar;
    import javax.swing.JOptionPane;
     
    /**
     *
     * @author Jay
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
            String strYear =
                    JOptionPane.showInputDialog(null,
                    "Enter a Year(e.g., 2001):",
                    "Input",
                    JOptionPane.QUESTION_MESSAGE);
            //Convert the string into an int value
            int Year = Integer.parseInt(strYear);
            
            String strStartDay =
                    JOptionPane.showInputDialog(null,
                    "Enter the month (1 thru 12):",
                    "Input",
                    JOptionPane.QUESTION_MESSAGE);
            //Convert the string into an int value
            int StartDay = Integer.parseInt(strStartDay);
            
            printMonth(Year, StartDay);
            }
        
            //------------------------------------------------------------
            static void printMonth(int year, int month) {
                printMonthTitle(year, month);
                printMonthBody(year, month);
            }
            
            //------------------------------------------------------------
            static void printMonthTitle(int year, int month){
                System.out.println("          " + getMonthName(month) + " " + year);
                System.out.println("----------------------------");
                System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
            }
            
            //------------------------------------------------------------
            static String getMonthName(int month){
                String monthName = null;
                switch (month) {
                    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 = "Semptember"; break;
                    case 10: monthName = "October"; break;
                    case 11: monthName = "November"; break;
                    case 12: monthName = "December"; break;
                }
                return monthName;
            }
            
            //------------------------------------------------------------
            static void printMonthBody(int year, int month) {
                int startDay = getStartDay(year, month);
                
                int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
                
                int i = 0;
                for (i = 0; i < startDay; i++)
                    System.out.print("    ");
                
                for (i = 1; i <= numberOfDaysInMonth; i++) {
                    if (i < 10)
                        System.out.print("   " + i);
                    else
                        System.out.print("  " + i);
                    
                    if ((i + startDay) % 7 == 0)
                        System.out.println();
                }
                
                System.out.println();
            }
            
            //------------------------------------------------------------
            static int getStartDay(int year, int month) {
                int startDay1800 = 3;
                int totalNumberOfDays = getTotalNumberOfDays(year, month);
                
                return (totalNumberOfDays + startDay1800) % 7;
            }
            
            //------------------------------------------------------------
            static int getTotalNumberOfDays(int year, int month) {
                int total = 0;
                
                for (int i = 1800; i < year; i++)
                    if (isLeapYear(i))
                        total = total + 366;
                    else
                        total = total + 365;
                
                for (int i = 1; i < month; i++)
                    total = total + getNumberOfDaysInMonth(year, i);
                
                return total;
            }
            
            //------------------------------------------------------------
            static int getNumberOfDaysInMonth(int year, int month) {
                if (month == 1 || month == 3 || month == 5 || month == 7 ||
                        month == 8 || month == 10 || month == 12)
                    return 31;
                
                if (month == 4 || month == 6 || month == 9 || month == 11)
                    return 30;
                
                if (month == 2)
                    return isLeapYear(year) ? 29 : 28;
                
                return 0;
                    
            }
            
            //------------------------------------------------------------
            static boolean isLeapYear(int year) {
                return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
            }
     
            }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Simply modify the main method as follows.

    Java Code:
        public static void main(String[] args) {
            
            String strYear =
                    JOptionPane.showInputDialog(null,
                    "Enter a Year(e.g., 2001):",
                    "Input",
                    JOptionPane.QUESTION_MESSAGE);
            //Convert the string into an int value
            int Year = Integer.parseInt(strYear);
            
            
            for(int i = 1; i < 13; i++) {
                printMonth(Year, i);
            }
       }
    It's better to read more about Java basis, if you can't really identify what's happening in this code. And also it's much better try something yourself first.

  3. #3
    jaydubya is offline Member
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Default

    Thank you very much Eranga.
    Can't believe I didn't see that one :rolleyes:
    Will rep some points to you when I post some more :D

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by jaydubya View Post
    Thank you very much Eranga.
    Can't believe I didn't see that one :rolleyes:
    Will rep some points to you when I post some more :D
    Reading the code carefully is the first thing we have to do, dealing with others codes. If you read it correctly, and if you know the basis correctly, no need to test the code on any IDE.

Similar Threads

  1. calendar
    By John in forum SWT / JFace
    Replies: 12
    Last Post: 08-07-2008, 11:54 PM
  2. How to use Calendar class
    By JavaBean in forum Java Tip
    Replies: 1
    Last Post: 06-26-2008, 04:09 PM
  3. How to Show Calendar
    By praveen.kb in forum AWT / Swing
    Replies: 2
    Last Post: 02-09-2008, 09:23 AM
  4. Help with gregorian calendar
    By osval in forum New To Java
    Replies: 2
    Last Post: 08-07-2007, 12:21 AM
  5. Web calendar
    By Daniel in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 06-27-2007, 06:36 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
  •