Results 1 to 10 of 10
- 02-28-2013, 08:11 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 63
- Rep Power
- 0
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; } }
- 03-01-2013, 12:48 AM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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:
The year 2012 does not resolve to true for this condition causing your application to execute the 'else' statement.Java Code:(year % 400 == 0)
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:
Regards.Java Code:for(int i = 1; i <=numberOfDays; i++)
- 03-01-2013, 09:07 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 63
- Rep Power
- 0
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
- 03-01-2013, 11:40 PM #4
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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.
- 03-02-2013, 01:05 AM #5
Member
- Join Date
- Feb 2013
- Posts
- 63
- Rep Power
- 0
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.
- 03-02-2013, 01:21 AM #6
Member
- Join Date
- Feb 2013
- Posts
- 63
- Rep Power
- 0
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.
- 03-02-2013, 01:50 PM #7
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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.
- 03-02-2013, 01:53 PM #8
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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.
- 03-02-2013, 04:39 PM #9
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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.
- 03-08-2013, 05:11 AM #10
Member
- Join Date
- Feb 2013
- Posts
- 63
- Rep Power
- 0
Similar Threads
-
problem displaying print margins in dialog
By simo_mon in forum Java 2DReplies: 2Last Post: 04-19-2012, 02:54 AM -
print with out displaying url
By srivempala in forum Advanced JavaReplies: 4Last Post: 05-13-2011, 03:28 PM -
Calender date discard/remove Z and time at the end of the calender date
By reach2sudhakar in forum Advanced JavaReplies: 18Last Post: 05-11-2011, 07:36 AM -
No output displaying
By Rgfirefly24 in forum New To JavaReplies: 6Last Post: 04-27-2008, 08:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks