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 1 of 1

Thread: Display Calendars in Console Program

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Display Calendars in Console Program

    I am creating a program that prompts the user to enter a year and the first day of the month, Sun=0, Mon=1, etc . . . then shows that years calendar with the correct number date placement, but I am having severe trouble getting the actual numbered dates up... Honestly I am just not quite sure how to do it. The calendars need to be displayed in the console. Any help is appreciated... I'm just lost.

    Here's what I have so far:
     
    import javax.swing.JOptionPane;
     
    public class test {
     
        public static void main(String[] args) {
     
     
     
     
           // Prompt the user to enter input
        String yearString = JOptionPane.showInputDialog("Enter a year:");
        int year = Integer.parseInt(yearString);
     
        String firstDayString = JOptionPane.showInputDialog("Enter the first day of the year:");
        int firstDay = Integer.parseInt(firstDayString);
     
        int numberOfDaysInMonth = 0;
     
     
     
        // Display calendar for each month
        for (int month = 1; month <= 12; month++) {
          // Display Calendar title
          switch (month) {
            case 1: System.out.printf("                        January\n");
                System.out.printf("=======================================================\n");
                System.out.printf("Sun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
                    numberOfDaysInMonth = 31;
                    break;
            case 2: System.out.printf("                        February\n");
                System.out.printf("=======================================================\n");
                System.out.printf("Sun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
                    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
                      numberOfDaysInMonth = 29;
                    else
                      numberOfDaysInMonth = 28;
            break;
            case 3: System.out.printf("                        March\n");
                System.out.printf("=======================================================\n");
                System.out.printf("Sun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 31;
            break;
            case 4: System.out.printf("                        April\n");
                System.out.printf("=======================================================\n");
                System.out.printf("Sun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 30;
            break;
            case 5: System.out.printf("                        May\n");
                System.out.printf("=======================================================\n");
                System.out.printf("Sun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 31;
            break;
            case 6: System.out.printf("                        June\n");
                System.out.printf("=======================================================\n");
                System.out.printf("Sun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 30;
            break;
            case 7: System.out.printf("                        July\n");
               System.out.printf("=======================================================\n");
                System.out.printf("Sun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 31;
            break;
            case 8: System.out.printf("                        August\n");
                System.out.printf("=======================================================\n");
                System.out.printf("Sun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 31;
            break;
            case 9: System.out.printf("                        September\n");
                System.out.printf("=======================================================\n");
                System.out.printf("Sun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 30;
            break;
            case 10: System.out.printf("                        October\n");
                System.out.printf("=======================================================\n");
                System.out.printf("Sun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 31;
            break;
            case 11: System.out.printf("                        November\n");
                System.out.printf("=======================================================\n");
                System.out.printf("Sun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 30;
            break;
            case 12: System.out.printf("                        December\n");
                System.out.printf("=======================================================\n");
                System.out.printf("Sun     Mon     Tues     Wed      Thurs     Fri     Sat\n");
            numberOfDaysInMonth = 31;
            break;
          }
     
     
    //      switch (firstDay) {
    //       
    //        case 0: System.out.println("Sunday"); break;
    //        case 1: System.out.println("Monday"); break;
    //        case 2: System.out.println("Tuesday"); break;
    //        case 3: System.out.println("Wednesday"); break;
    //        case 4: System.out.println("Thursday"); break;
    //        case 5: System.out.println("Friday"); break;
    //        case 6: System.out.println("Saturday"); break;
    //      }
    //      // Get the start day for the next month
    firstDay = (firstDay + numberOfDaysInMonth) % 7;
        }
      }
    }

    I just made this which displays the right format for one month... but if i kept this method up it would take up so many lines of code. My professor said that it must be done in under 150 lines.

    public class NewClass {
     public static void main(String[] args) {
     
     for(int num1 = 1; num1 <= 7; num1++){
            System.out.print(num1 + "        ");
        }
         System.out.println("\n");
        for(int num2 = 8; num2 <= 14; num2++){
        System.out.print(num2+"       ");
        }
        System.out.println("\n");
        for(int num3 = 15; num3 <= 21; num3++){
            System.out.print(num3+"       ");
        }
         System.out.println("\n");
        for(int num4 = 22; num4 <= 28; num4++){
            System.out.print(num4+"       ");
        }
         System.out.println("\n");
        for(int num5 = 29; num5 <= 31; num5++){
            System.out.print(num5+"       ");
        }
     
            }
    }
    Last edited by ColeTrain; October 14th, 2012 at 07:12 PM.


Similar Threads

  1. Introduction Tools for generate service from java console program / ?
    By navidnmc in forum Java Theory & Questions
    Replies: 0
    Last Post: October 6th, 2012, 02:00 AM
  2. [SOLVED] Arrow keys to navigate a maze in a console program
    By B25Mitch in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 31st, 2012, 04:58 PM
  3. How do you make a console program a .jar in Eclipse?
    By Programmer142 in forum Java Theory & Questions
    Replies: 9
    Last Post: January 13th, 2012, 12:13 AM
  4. SWING GUI CONSOLE WINDOW DISPLAY
    By Khadafi in forum Java Theory & Questions
    Replies: 5
    Last Post: January 10th, 2012, 09:15 AM
  5. simple java console program, need help recalling commands
    By zero0000000 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 22nd, 2010, 05:47 AM