Results 1 to 10 of 10
  1. #1
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Attempting to print a calender put my output is displaying incorrectly

    This is what it should look like (a bit more aligned)
    Feb 2012
    Su Mo Tu We Th Fr Sa
    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

    what I am getting is:

    Feb 2012
    Su Mo Tu We Th Fr Sa
    1
    2 3 4 5 6 7 8
    ......
    .....
    ...... 27

    There seems to be a problem with the check for leap year and displaying the no. of days for feb because it should be 29 for 2012 as it is a leap year.
    In general I think there is a problem with the no. of days displaying.



    Java Code:
    import java.util.Scanner;
    
    public class Calender_b
    {
      public static void main(String[] args)
      {
      Scanner input = new Scanner(System.in);
    
        //Prompt user for month and year
    
        System.out.print("Enter Month (1 - 12): ");
        int month = input.nextInt();
            while (month < 1 || month > 12 )
             {
              System.out.print("Enter Month (1 - 12): ");
               month = input.nextInt();
             }
            
        System.out.print("Enter Year (1900 - 2100): ");
        int year = input.nextInt();
            while (year < 1900 || year > 2100 )
             {
              System.out.print("Enter Year (1900 - 2100): ");
               year = input.nextInt();
             }   
    
       printMonth(month, year);
      } 
    
      public static void printMonth(int month, int year)
      {
      
        printMonthTitle(month, year);
        
        printMonthBody(month, year);
    
      }
    
      public static void printMonthTitle(int month, int year)
      {
        System.out.println("");
        System.out.println(" "+getMonthName(month) +"             "+ year);
        System.out.print("");
        System.out.println(" Su Mo Tu We Th Fr Sa ");
        
    
      }
    
      public static String getMonthName(int month)
      {
    
          String monthString = "";
        
          switch (month) 
          {
                case 1:  monthString = "Jan";
                         break;
                case 2:  monthString = "Feb";
                         break;
                case 3:  monthString = "Mar";
                         break;
                case 4:  monthString = "Apr";
                         break;
                case 5:  monthString = "May";
                         break;
                case 6:  monthString = "Jun";
                         break;
                case 7:  monthString = "Jul";
                         break;
                case 8:  monthString = "Aug";
                         break;
                case 9:  monthString = "Sep";
                         break;
                case 10: monthString = "Oct";
                         break;
                case 11: monthString = "Nov";
                         break;
                case 12: monthString = "Dec";
                         
         }
       
       return monthString;
    
      }
      
      public static void printMonthBody(int month, int year)
      {
         int startDay = getStartDay(month, year);
         int numberOfDays = getNumOfDaysInMonth(month, year);
        
         int i = 0;
         for (i = 0; i < startDay; i++)
           {
             System.out.print("  ");
           }
    
         for (i = 1; i < numberOfDays; i++)
           {
             System.out.printf("%3d", i);
           
             if((i + startDay) % 7 ==  0)
             {
              System.out.println();
             }
           }
        System.out.println("");
      }
       
      public static int getStartDay(int month, int year)
      {
         // declare variables needed for Rev.Zeller's algorithm
         int a = month, b = 1, c = year, d = year / 100 + 1;
          
         //compute the required quantities
           
           int w = ( 13 * a - 1 ) / 5;
           int x = c / 4;
           int y = d / 4;
           int z = w + x + y + b + c - 2 * d;
           int r = 0;
               if ( z > 0)
                {
                 r = z % 7;
                }
               else
                {
                 r = (r + 7) % 7;
                } 
      
         return r;
      }
    
      public static int getNumOfDaysInMonth(int month, int year)
      {
         int numDays = 0;
    
         //Find the no. of days in a month
       if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        {
          numDays = 31;
        } 
    
       else if (month == 2)
        {
          
         if ( (year % 4 == 0) && (year % 400 == 0) && !(year % 100 == 0) )
          {
            numDays = 29;
          }
          else
          {
            numDays = 28;
          }
        }
       else
        {
         numDays = 30;
        } 
       return numDays;
      }
    
    
    }

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Attempting to print a calender put my output is displaying incorrectly

    Hi abi, there are two issues with your code.

    Firstly, the 'if' statement on line 146 contains the following code:
    Java Code:
    (year % 400 == 0)
    The year 2012 does not resolve to true for this condition causing your application to execute the 'else' statement.

    Secondly, your 'for' loop on line 97 loops whilst 'i' is less than 'numberOfDays' causing your application to print out everything but the last day.
    The easiest way to correct this is with the following:
    Java Code:
    for(int i = 1; i <=numberOfDays; i++)
    Regards.

  3. #3
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Attempting to print a calender put my output is displaying incorrectly

    I made the correction for the if on line 146 by putting and || instead of an &&.
    And as for the for statement I made that correction soon after I posted this but it still shows Feb 2012 in that incorrect format with the days going up to 28.

    Here is the link to the criteria of the assignment if you wanna look at it.
    Programming Assignment 5

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Attempting to print a calender put my output is displaying incorrectly

    Abi,

    For the formatting have a look at number formatting. This can be done directly to the output...
    Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

    ...or a string.
    Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

    Your 'if' statement on line 146 needs some more work.
    Have a look at the conditions for which Febuary has 29 days. Without looking at the latest code, you were mostly there with your initial attempt.

    Regards.

  5. #5
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Attempting to print a calender put my output is displaying incorrectly

    I've made changes to the code. And I am using the printf but I am still having trouble with the days output. The program complies but now I get some sort of a huge error message that says IllegalFOrmatConversion.
    Java Code:
    import java.util.Scanner;
    
    public class Calendar_b
    {
      public static void main(String[] args)
      {
      Scanner input = new Scanner(System.in);
    
        //Prompt user for month and year
    
        System.out.print("Enter Month (1 - 12): ");
        int month = input.nextInt();
            while (month < 1 || month > 12 )
             {
              System.out.print("Enter Month (1 - 12): ");
               month = input.nextInt();
             }
            
        System.out.print("Enter Year (1900 - 2100): ");
        int year = input.nextInt();
            while (year < 1900 || year > 2100 )
             {
              System.out.print("Enter Year (1900 - 2100): ");
               year = input.nextInt();
             }   
           
    
       printMonth(month, year);
      } 
    
      public static void printMonth(int month, int year)
      {
      
        printMonthTitle(month, year);
        
        printMonthBody(month, year);
    
      }
    
      public static void printMonthTitle(int month, int year)
      {
    
        System.out.println("");
        System.out.println(" "+getMonthName(month) +"             "+ year);
        System.out.print("");
        System.out.println(" Su Mo Tu We Th Fr Sa ");
        
      }
    
      public static String getMonthName(int month)
      {
    
          String monthString = "";
        
          switch (month) 
          {
                case 1:  monthString = "Jan";
                         break;
                case 2:  monthString = "Feb";
                         break;
                case 3:  monthString = "Mar";
                         break;
                case 4:  monthString = "Apr";
                         break;
                case 5:  monthString = "May";
                         break;
                case 6:  monthString = "Jun";
                         break;
                case 7:  monthString = "Jul";
                         break;
                case 8:  monthString = "Aug";
                         break;
                case 9:  monthString = "Sep";
                         break;
                case 10: monthString = "Oct";
                         break;
                case 11: monthString = "Nov";
                         break;
                case 12: monthString = "Dec";
                         
         }
       
       return monthString;
    
      }
      
      public static void printMonthBody(int month, int year)
      {
         int startDay = getStartDay(month, year);
         int numberOfDays = getNumOfDaysInMonth(month, year);
        
         int i = 0;
         for (i = 0; i < startDay; i++)
           {
             System.out.printf("%3s","");
           }
    
         for (i = 1; i <= numberOfDays; i++)
           {
             System.out.printf("%3d", i);
           
             if((i + startDay) % 7 ==  0)
             {
              System.out.println();
             }
           }
        System.out.println();
      }
      
      public static int changeMonth(int month)
      {
        int monthX = 0; 
      
        switch (month)
        {
                case 1:  monthX = 11;
                         break;
                case 2:  monthX = 12;
                         break;
                case 3:  monthX = 1;
                         break;
                case 4:  monthX = 2;
                         break;
                case 5:  monthX = 3;
                         break;
                case 6:  monthX = 4;
                         break;
                case 7:  monthX = 5;
                         break;
                case 8:  monthX = 6;
                         break;
                case 9:  monthX = 7;
                         break;
                case 10: monthX = 8;
                         break;
                case 11: monthX = 9;
                         break;
                case 12: monthX = 10;
        }                     
       return monthX;
      }
    
       
      public static int getStartDay(int month, int year)
      {
         int m = changeMonth(month);
    
       // declare variables needed for Rev.Zeller's algorithm
         int a = m, b = 1; 
         int c = 0;
          if (m == 11 || m == 12)
            {
              c = year - 1;
            }
           else 
            {
              c = year;
            }
         int d = 0;
         if(month <= 12 && year == 1900)
          {
            d = 19;  
          }
         else if ( year <= 2000 )
          {
            d = 20;
          }
         else
           {
             d = 21;
           }
          
         //compute the required quantities
           
           int w = ( 13 * a - 1 ) / 5;
           int x = c / 4;
           int y = d / 4;
           int z = w + x + y + b + c - 2 * d;
           int r = 0;
               if ( z > 0)
                {
                 r = z % 7;
                }
               else
                {
                 r = (r + 7) % 7;
                } 
           
      
         return r;
      }
    
      public static int getTotalNumberOfDays(int month, int year)
      {
        int total = 0;
       
        for(int i = 1900; i < year; i++)
         {
           if( (year % 4 == 0) || (year % 400 == 0) && !(year % 100 == 0) )
            {
              total += 366;
            }
           else
            {
              total += 365;
            }
         }
     
         for(int i = 1; i < month; i++)
          {
            total = total + getNumOfDaysInMonth(year, i);  
          }
         return total;
      }   
    
      public static int getNumOfDaysInMonth(int month, int year)
      {
         int numDays = 0;
    
         //Find the no. of days in a month
       if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        {
          numDays = 31;
        } 
    
       else if (month == 2)
        {
          
         if ( (year % 4 == 0 && year % 100!= 0) || (year % 400 == 0)  )
          {
            numDays = 29;
          }
          else
          {
            numDays = 28;
          }
        }
       else
        {
         numDays = 30;
        } 
       return numDays;
      }
    
    
    }
    Last edited by abi; 03-02-2013 at 01:39 AM.

  6. #6
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Attempting to print a calender put my output is displaying incorrectly

    This is the error
    Feb 2012
    Su Mo Tu We Th Fr Sa
    Exception in thread "main" java.util.UnknownFormatConversionException: Conversio
    n = '3'
    at java.util.Formatter.checkText(Formatter.java:2547)
    at java.util.Formatter.parse(Formatter.java:2533)
    at java.util.Formatter.format(Formatter.java:2469)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at Calendar_b.printMonthBody(Calendar_b.java:95)
    at Calendar_b.printMonth(Calendar_b.java:36)
    at Calendar_b.main(Calendar_b.java:28)
    Last edited by abi; 03-02-2013 at 01:36 AM.

  7. #7
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Attempting to print a calender put my output is displaying incorrectly

    Hi abi,

    To format the output you need to use format() as opposed to printf(). Have another look at the first link I posted in #4, you can see an example about half way down.

    Regards.

  8. #8
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Attempting to print a calender put my output is displaying incorrectly

    Ignore #7, printf() and format() seem to do the same thing. I'll have another look at the code and get back to you.

    Regards.

  9. #9
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Attempting to print a calender put my output is displaying incorrectly

    The error is complaining about line 95 but this code runs ok on my machine with the ex.
    What jdk / ide are you currently using?

    Regards.

  10. #10
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Attempting to print a calender put my output is displaying incorrectly

    Thanks for the help, and sorry for the late reply. I figured out what the problem was, it had to do with the parameters for printf().

    Thanks

Similar Threads

  1. problem displaying print margins in dialog
    By simo_mon in forum Java 2D
    Replies: 2
    Last Post: 04-19-2012, 02:54 AM
  2. print with out displaying url
    By srivempala in forum Advanced Java
    Replies: 4
    Last Post: 05-13-2011, 03:28 PM
  3. Replies: 18
    Last Post: 05-11-2011, 07:36 AM
  4. No output displaying
    By Rgfirefly24 in forum New To Java
    Replies: 6
    Last Post: 04-27-2008, 08:37 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
  •