error with zeller's congruence code - java

public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the month (1-12):");
int month = input.nextInt();
System.out.print("Enter the day (1-31):");
int day = input.nextInt();
System.out.print("Enter the year (ex. 2014):");
int year = input.nextInt();
if (month == 1)
month = 13;
else if (month == 2)
month = 14;
int Zeller = day + ((26 * (month + 1)) / 10) + year +
(year / 4) + (6 * (year / 100)) + (year / 400);
int dayofweek = (Zeller + 5) % 7 + 1;
if (dayofweek == 1)
System.out.print("Day of the week: Monday");
else if (dayofweek == 2)
System.out.print("Day of the week: Tuesday");
else if (dayofweek == 3)
System.out.print("Day of the week: Wednesday");
else if (dayofweek == 4)
System.out.print("Day of the week: Thursday");
else if (dayofweek == 5)
System.out.print("Day of the week: Friday");
else if (dayofweek == 6)
System.out.print("Day of the week: Saturday");
else if (dayofweek == 7)
System.out.print("Day of the week: Sunday");
}
The code wont calculate the dates in Jan and Feb correctly. It is always a day or two off.
For example: If I enter Jan 1, 2010 the code should output Day of the week: Friday, but instead, its saying its Day of the week: Saturday.
I've checked my calculations couldn't find any error.

According to the Zeller's congruence wiki page, when you adjust the January and February months, you must also adjust the year down, because the algorithm relies on January and February being considered the "13th" and "14th" months of the previous year.
But when using a computer, it is simpler to handle the modified year Y, which is Y - 1 during January and February:
Add the year adjustment code to your January and February cases.
if (month == 1)
{
month = 13;
year--; // add this
}
else if (month == 2)
{
month = 14;
year--; // add this
}
Output for your test case of January 1st, 2010:
Enter the month (1-12):1
Enter the day (1-31):1
Enter the year (ex. 2014):2010
Day of the week: Friday
The cases above could be simplified:
if (month == 1 || month == 2)
{
month += 12;
year--;
}

Related

I am using Boolean variables to print the number of days in a calendar days. The outputs aren't what I am expecting

I am trying to write a program which shows the number of days in a given month of a year. The outputs for the month of February, non leap years (no output) and the months having 30 days in leap years is incorrect (output is 29). How could I resolve this?
public class Q11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner number1 = new Scanner(System.in);
Scanner number2 = new Scanner(System.in);
System.out.println("Enter the month (1-12)");
int month = number1.nextInt();
System.out.println("Enter the year");
int year = number2.nextInt();
int number_of_days = 0;
boolean N1 = (month == 1) ||
(month == 3) ||
(month == 5) ||
(month == 7) ||
(month == 8) ||
(month == 10) ||
(month == 12);
boolean N2 = (month == 2) && (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
boolean N3 = (month == 2) && (year % 400 <= 1) || (year % 4 <= 1) && (year % 100 == 0);
boolean N4 = (month == 4) ||
(month == 6) ||
(month == 9) ||
(month == 11);
if (N1)
System.out.println(". This month has 31 days");
else if (N2)
System.out.println(". This month has 29 days");
else if (N3)
System.out.println(". This month has 28 days");
else if (N4)
System.out.println(". This month has 30 days");
}
}
Problem
You run into a problem due to the fact how the program evaluates your boolean expressions. Remember the rules for boolean precedence. && is evaluated first and takes precedence. Also, the formula is evaluated from left to right. Operator precedence can be overruled with the correct setting of parentheses.
E.g. if you have a && b || c, a && b is evaluated first, after which we evaluate its result against || c. But: a && (b || c) is evaluated by first evaluating b || c and then evaluating this result against && a.
In your case, when you enter e.g. month = 4 and year = 2020 the following happens:
The rule n1 is skipped - month is not listed.
The rule n2 is evaluated - month is not equals to two and the year is not divisible by 400; so far everything goes to plan. But now - the next || comes around to bite you. The year is divisible by 4 AND it is not divisible by 100. The program now incorrectly prints 29.
The rules n3 and n4 are solved like n2 and are left as an exercise for the reader :)
Solution
Easiest way is to understand why n2 is failing and optimize the formula. Since this is a homework task I don't want to solve it for you.
General
One scanner instance on System.in is enough.
Your variable names should follow the Java naming convention, lowerCaseCamelCase instead of snake_case and N1.
Parting thought
Try using a switch statement on month - it will make your code a bit easier to understand. Finally: Good luck in your studies.
In your existing code make the following changes.
this checks for leap year.
if it is divisible by 4 and not a century year, its is a leap year
or if is divisible by 400 it is a leap year.
otherwise, it isn't.
boolean N2 = (year %4 == 0 && year %100 != 0) || (year % 400 == 0);
Now just use the negation of the boolean to test for non leap year.
if (N1)
System.out.println(". This month has 31 days");
else if (N4) {
System.out.println(". This month has 30 days");
} else if (month == 2) {
boolean N2 = (year %4 == 0 && year %100 != 0) || (year % 400 == 0);
if (N2) {
System.out.println("This month has 29 days");
} else {// must not be leap year.
System.out.println("This month has 28 days");
}
} else {
System.out.printf("Bad month(%d) entered", month);
}
Also, you should only use one Scanner for all console input.
An alternative approach
In case you didn't know, the enhanced switch statement introduced in Java 13 would be perfect for this.
it takes multiple cases
and returns an argument for processing.
public class Q11 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the month (1-12)");
int month = scanner.nextInt();
System.out.println("Enter the year");
int year = scanner.nextInt();
boolean isLeapYear = (year %4 == 0 && year %100 != 0) || (year % 400 == 0);
String proclamation = switch(month) {
case 1,3,5,7,8,10,12 -> "This month has 31 days.";
case 4,6,9,11 -> "This month has 30 days";
case 2 -> "This month has %d days".formatted(isLeapYear ? 29 : 28);
default -> "Bad month (%d) entered.".formatted(month);
};
System.out.println(proclamation);
}
}

How to handle invalid input

Can anyone suggest an example on how to handle invalid inputs, I have got the logic wrong for input from the keyboard. So, I need !myScanner.hasNextInt() or to put that loop in a method which I can reuse for ever.
package dayoftheyear;
import java.util.Scanner;
/**
*
* #author abdal
*/
public class DayOfTheYear {
/**
* Ask the user to enter a month, day, and year as integers
* separated by spaces then display the number of the days since the
* beginning of the year.
* Don’t forget about leap year.
* Sample: user inputs ‘3 1 2000’, output is ‘61’.
* #param args Unused.
*/
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String enter = "A-z";
int a=31;
int b=1;
boolean dateCheck;
int month;
int day;
int year;
do {
System.out.print("Enter a valid month day year separated by spaces");
if (s.hasNextInt()) {
month= s.nextInt();
day=s.nextInt();
year=s.nextInt();
if (month >= b && month <= a || day>=b && day<=a || year>=b) {
int numberOfDay = countDays(month, day, year);
System.out.println(+ month + "/" + day + "/" + year + " is a day number "
+ numberOfDay + " of that year");
dateCheck = true;
} else {
System.out.println("Enter a valid month day year separated by spaces");
dateCheck = false;
}
} else {
System.out.println("Not a date");
month = 0;
day=0;
year=0;
s.next();
dateCheck = false;
}
} while (!dateCheck);
/**
* Get the number of days since the start of the year.
* Declare a 12 element array and initialize it with the number of
* days in each month.
* #param month Month to count from.
* #param day Day of the month to count to.
* #param year The year to count from.
* #return Days since the start of the given year.
*/
} public static int countDays(int month, int day, int year) {
int monthLength[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
int days = 0;
if (isLeapYear(year) && month > 2)
days += 1;
for (int i = 0; i < month - 1; i++)
days += monthLength[i];
return days += day;
}
/**
* Check if a year is a leap year.
* #param year Year to check.
* #return True if the year is a leap year.
*/
public static boolean isLeapYear(int year) {
if (year % 4 != 0) return false;
else if (year % 100 != 0) return true;
else return year % 400 == 0;
}
}
Replace
if (month >= b && month <= a || day>=b && day<=a || year>=b)
with
if (month >= 1 && month <= 12 && day >= 1 && day <= 31 && year >= 1)
A sample run after this change:
Enter a valid month day year separated by spaces: 13 1 2000
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 3 32 2000
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 3 1 2
3/1/2 is a day number 60 of that year
Update1: the following condition will hold good also for February
if ((month == 2 && day >= 1 && day <= 28 && year >= 1 && !isLeapYear(year))
|| (month == 2 && day >= 1 && day <= 29 && year >= 1 && isLeapYear(year))
|| (month != 2 && month >= 1 && month <= 12 && day >= 1 && day <= 31 && year >= 1))
A sample run:
Enter a valid month day year separated by spaces: 2 29 2001
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 13 2 2001
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 1 32 2001
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 1 2 3
1/2/3 is a day number 2 of that year
Update2: the following code addresses all the issues raised in the comments
int monthLength[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ((month == 2 && day >= 1 && day <= 28 && year >= 1 && !isLeapYear(year))
|| (month == 2 && day >= 1 && day <= 29 && year >= 1 && isLeapYear(year))
|| (month != 2 && month >= 1 && month <= 12 && day >= 1 && day <= monthLength[month-1] && year >= 1))
A sample run:
Enter a valid month day year separated by spaces: 2 29 2001
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 13 2 2001
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 1 32 2001
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 6 31 2020
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 7 31 2020
7/31/2020 is a day number 213 of that year
Since date format is not subject to change in real world, it should be appropriate to use some hard coding. Dividing the problem into several methods is always recommended.
You could add following methods to your class:
private static boolean validDate(int month, int day, int year) {
if (year < 1) {
return false; // no B.C.
}
if (month > 1 && month < 13) {
if (month == 2) { // handle February
return validFeb(day, year);
} else if (month % 2 == 1 && month < 8
|| month % 2 == 0 && month >= 8) { // 31-day months
return valid31(day);
} else { // 30 day months
return valid30(day);
}
}
return false;
}
Validate 30-day months:
private static boolean valid30(int day) {
if (day > 1 && day < 31) {
return true;
}
return false;
}
Validate 31-day months
private static boolean valid31(int day) {
if (day > 1 && day < 32) {
return true;
}
return false;
}
Validate February
private static boolean validFeb(int day, int year) {
if (isLeapYear(year)) {
if (day > 1 && day < 30) {
return true;
}
} else {
if (day > 1 && day < 29) {
return true;
}
}
return false;
}
Then your do-while loop should look something like so:
do {
System.out.print("Enter a valid month day year separated by spaces\n");
if (s.hasNextInt()) month = s.nextInt();
else s.next();
if (s.hasNextInt()) day = s.nextInt();
else s.next();
if (s.hasNextInt()) year = s.nextInt();
else s.next();
int numberOfDaysSinceStart = 0;
if (validDate(month, day, year)) {
dateCheck = true;
numberOfDaysSinceStart = countDays(month, day, year);
System.out.println(month + "/" + day + "/" + year + " is a day number "
+ numberOfDaysSinceStart + " of that year");
} else {
dateCheck = false;
}
} while (!dateCheck);
You can add edge cases forever. There is a reason why time-related calculations are nightmare outsourced to libraries written by some poor souls that are paid to try to cover them all. Java has such built in, take a look at java.util.Calendar (Gregorian implementation).
You set it to year/month/day, and it will puke out an exception if anything is wrong while trying to get the result.
Calendar c = Calendar.getInstance();
c.set(year, month, day);
try {
c.getTime();
} catch (Exception e) {
// wrong date format
}
I think the code below can help you, comment if you have any questions.
while(true) {
try{
System.out.print("Enter a valid month day year separated by spaces");
month= s.nextInt();
day=s.nextInt();
year=s.nextInt();
if (month >= 1 && month <= 12 || day>=1 && day<=31 || year>=1) {
System.out.println(+ month + "/" + day + "/" + year + " is a day number "+ " of that year");
break;
} else {
System.out.println("Enter a valid month day year separated by spaces");
}
} catch(InputMismatchException e) {
System.out.println("Enter a valid month day year separated by spaces");
}
s.next();
}
thanks I updated my program and it is working;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;
boolean dateCheck;
do {
System.out.print("Enter a valid month day year separated by spaces\n");
if (s.hasNextInt()) month = s.nextInt();
else s.next();
if (s.hasNextInt()) day = s.nextInt();
else s.next();
if (s.hasNextInt()) year = s.nextInt();
else s.next();
int numberOfDaysSinceStart = 0;
if (month >= 1 && month <= 12 && day >= 1 && day <= 31 && year >= 1) {
dateCheck = true;
numberOfDaysSinceStart = countDays(month, day, year);
System.out.println(month + "/" + day + "/" + year + " is a day number "
+ numberOfDaysSinceStart + " of that year");
} else {
dateCheck = false;
}
} while (!dateCheck);
/**
* Get the number of days since the start of the year.
* Declare a 12 element array and initialize it with the number of
* days in each month.
* #param month Month to count from.
* #param day Day of the month to count to.
* #param year The year to count from.
* #return Days since the start of the given year.
*/
} public static int countDays(int month, int day, int year) {
int monthLength[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
int days = 0;
if (isLeapYear(year) && month > 2)
days += 1;
for (int i = 0; i < month - 1; i++)
days += monthLength[i];
return days += day;
}
/**
* Check if a year is a leap year.
* #param year Year to check.
* #return True if the year is a leap year.
*/
public static boolean isLeapYear(int year) {
if (year % 4 != 0) return false;
else if (year % 100 != 0) return true;
else return year % 400 == 0;
}
}

Date calculator: Tell what day of the week a certain date is

I am trying to write a program in Java (this is a school assignment that tells you what day of the week a certain date is. (The date should be written on the form yyyy-mm-dd.) I thought I had come up with a solution with the code below, but then I found an error.
When you run the code, and type in 1999-12-31 in the dialog, the program tells you that the entered date (1999-12-31) is a Friday. But when you type in the date 2000-01-01 (which is one day after 1999-12-31), the program tells you that the day is a Sunday! What happened with Saturday? A similar problem happens when you type in 2000-02-29 and 2000-03-01, they both give Wednesday as an answer!
What I have yet noticed, this error appears only when you enter a date between 2000-01-01 and 2000-02-29. I would be very grateful if someone could please help me to find the cause of the error and to solve the problem!
import static javax.swing.JOptionPane.*;
import static java.lang.Math.*;
public class DateCalc {
// Here, between the DateCalc class declaration and the main method, several methods used in the program are
// constructed.
// The method isLeapYear tests whether the entered year is a leap year or not.
private static boolean isALeapYear(int year) {
// If the year is a multiple of 4 and not a multiple of 100, or a multiple of 400, then it is a leap year.
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return true;
}
else {
return false;
}
}
// A method that tests whether a given string is written as a valid date.
private static boolean isAValidDate(int year, int month, int day) {
int maxValidYear = 9999;
int minValidYear = 1754;
if (year > maxValidYear || year < minValidYear) {
return false;
}
if (month < 1 || month > 12) {
return false;
}
if (day < 1 || day > 31) {
return false;
}
// Handle the February Month
if (month == 2) {
if (isALeapYear(year)) {
return (day <= 29); // This statement is true if the value of the day is less than or equal to 29 if the month is February within a leap year.
// Otherwise the statement is false and the method will return the boolean value false.
}
else {
return (day <= 28); // This statement is true if the value of the day is less than or equal to 28 if the month is February within a non-leap year.
// Otherwise the statement is false and the method will return the boolean value false.
}
}
// Month of April, June, September and November must have number of days less than or equal to 30.
if (month == 4 || month == 6 || month == 9 || month == 11) {
return (day <= 30);
}
return true;
}
// A method that calculates the day number within the year.
private static int dayNumberWithinYear(int year, int month, int day) {
// An array which stores the number of days in the different months (when the year is not a leap year).
// (Index 0 is the number of days in January, index 1 is the number of days in February, etc.)
int[] monthStructure = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// If the entered year is a leap year, then the monthStructure array will be initialized with an extra day in February, i.e the leap day.
if (isALeapYear(year)) {
monthStructure[1] = 29;
}
int sumDaysInPreviousMonths = 0;
int daysInTheCurrentMonth = day;
int dayNumber = 0;
// Loops through all the months (index 0 is January, index 1 is February, etc.).
for (int i = 0; i < month - 1; i++) {
sumDaysInPreviousMonths += monthStructure[i];
}
dayNumber = sumDaysInPreviousMonths + daysInTheCurrentMonth;
return dayNumber;
}
// A method that decides the day of the week of an entered date.
private static void weekDay(int year, int month, int day) {
// The number of days that have passed since January 1, 1754, excluding the days of the entered year and
// excluding the leap days.
int sumDaysInOrdinaryYears = (year - 1754) * 365;
int sumLeapDaysInLeapYears = 0;
// Suppose the entered year is n. The for-loop goes through all the years from year n-1 to year 1754, and
// checks if the current year in the loop is a leap year. The number of leap years between year 1754 and n-1
// is equal to the number of days that will get added (beside from the days in ordinary years) to the total
// days from January 1, 1754 to the entered date.
for (; year > 1754; year -= 1) {
if (isALeapYear(year)) {
sumLeapDaysInLeapYears += 1;
}
}
// The sum of all days from year 1754 to year n-1 (if the entered year is n), is equal to the sum of days in
// the ordinary years and the leap days in the years.
int sumDaysInEveryYearExcludingTheEntered = sumDaysInOrdinaryYears + sumLeapDaysInLeapYears;
int sumDaysInTotalYears = sumDaysInEveryYearExcludingTheEntered + dayNumberWithinYear(year, month, day);
int weekDay = sumDaysInTotalYears % 7;
if (weekDay == 0) {
showMessageDialog(null, "The date is a monday.");
}
else if (weekDay == 1) {
showMessageDialog(null, "The date is a tuesday.");
}
else if (weekDay == 2) {
showMessageDialog(null, "The date is a wednesday.");
}
else if (weekDay == 3) {
showMessageDialog(null, "The date is a thursday.");
}
else if (weekDay == 4) {
showMessageDialog(null, "The date is a friday.");
}
else if (weekDay == 5) {
showMessageDialog(null, "The date is a saturday.");
}
// If weekDay == 6
else {
showMessageDialog(null, "The date is a sunday.");
}
}
public static void main(String[] args) {
// This is step 3 in the laboratory instruction.
while (true) {
String date = showInputDialog("Please, enter a date on the form yyyy-mm-dd");
// If the user clicks 'Cancel' or clicks 'OK' when the dialog box is empty, the program will exit.
if (date == null || date.length() == 0) {
break;
}
int y = Integer.parseInt(date.substring(0,4));
int m = Integer.parseInt(date.substring(5,7));
int d = Integer.parseInt(date.substring(8));
if (!isAValidDate(y, m, d)) {
showMessageDialog(null, "Error! The entered date is invalid. " +
" Please enter a valid date on the form yyyy-mm-dd");
}
else {
weekDay(y, m, d);
}
}
}
}
Instead of asking us to debug through your entire code, perhaps consider LocalDate to get the desired result:
LocalDate ldt = LocalDate.parse("1999-12-31");
System.out.println(ldt.getDayOfWeek());
LocalDate ldt2 = LocalDate.parse("2000-01-01");
System.out.println(ldt2.getDayOfWeek());
Output:
FRIDAY
SATURDAY
The Problem is with finding the number of leap year. Your logic is counting the year 2000 also. The number of leap years should be same for 1999-12-31 and 2000-01-01. You need to consider year 2000 only if the month is greater than February. Increment the sumLeapDaysInLeapYears only if the input date is greater than Feb 28th

Java leap year code problems

import java.util.Scanner;
public class Hw2JamesVaughn {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = input.nextInt();
if((year < 1582) == (year % 4==0))
System.out.println(year + " is a leap year");
else
System.out.println(year + " is not a leap year");
if((year > 1582) == (year % 100 != 0) || (year % 400 == 0))
System.out.println(year + " is a leap year");
else
System.out.println(year + " is not a leap year");
}
}
This is the assignment.
(To determine if a particular year is a leap year, use the following logic:
the year must be divisible by 4
starting from 1582, if the year is divisible by 100, it must also be divisible by 400
Thus, the year 1700 is not a leap year, but 2000 is. However, 1500 is leap year since it was
before 1582, the adoption year of Gregorian calendar.
Your program will ask for a year, and then display whether the year is leap year or not.)
I have gotten this far with my java leap year program but its not working! Ive been working on this and i have no idea what is wrong.
Firstly, this if((year < 1582) == (year % 4==0)) checks boolean equality. I think you wanted an if((year < 1582) && (year % 4==0)) but I'm afraid that still doesn't fix your logic.
I suggest you start by creating a method. The first part should test if the year is less then 1582. If so, return true if it's a multiple of 4. The second part is well described on Wikipedia here. Putting it together gives something like,
private static boolean isLeapYear(int year) {
if (year < 1582) {
return (year % 4 == 0);
}
/*
* Rest of algorithm from: http://en.wikipedia.org/wiki/Leap_year
*/
if (year % 4 != 0) {
/*
* if (year is not divisible by 4) then (it is a common year)
*/
return false;
} else if (year % 100 != 0) {
/*
* else if (year is not divisible by 100) then (it is a leap year)
*/
return true;
}
/*
* else if (year is not divisible by 400) then (it is a common year)
* else (it is a leap year)
*/
return (year % 400 == 0);
}
Then you can use printf to output the result,
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = input.nextInt();
System.out.printf("%d %s leap year", year, isLeapYear(year) ? "is a"
: "is not a");
}
Finally, your original code could be implemented like -
if (year < 1582 && year % 4 == 0)
System.out.println(year + " is a leap year");
else if (year < 1582)
System.out.println(year + " is not a leap year");
else if (year >= 1582 && (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)))
System.out.println(year + " is a leap year");
else
System.out.println(year + " is not a leap year");
Apart from the algorithm you can calculate leap year using java's built in Calendar api.
static boolean isLeapYear(int year){
Calendar calendar= Calendar.getInstance();
calendar.set(Calendar.YEAR,year);
return calendar.getActualMaximum(Calendar.DAY_OF_YEAR) > 365;
}

Mistake in my code and lucky answer

Spoiler alert!! for project euler problem 19.
The problem is how many sundays fall on the first of the month from 1901 to 2000. Given information: Jan 1st 1900 was a monday, Jan & march etc have 31 days, April, June etc 30 days and a leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. which in this case applies to year 2000.
I got the correct answer, but when I checked my results with a real calendar, it turns out my code is counting saturdays. I don't see why it is doing that, can someone help?!
public class Sunday {
public static void main(String[] args) {
int sundayCount = 0;
int currentday = 0; // monday is 0, sunday will be 6
int dayLimit = 0;
for (int year = 1900; year < 2001; year++) {
for (int month = 1; month < 13; month++) {
// 30 days April, June, September, November
if (month == 4 || month == 6 || month == 9 || month == 11)
dayLimit = 30;
// leap year
else if (month == 2 && year % 4 == 0)
dayLimit = 29;
// February not leap year
else if (month == 2 && year % 4 != 0)
dayLimit = 28;
// Jan, March, May, July, August, October, December
else
dayLimit = 31;
for (int day = 1; day <= dayLimit; day++) {
if (day == 1 && currentday == 6 && year > 1900) {
System.out.println("year: " + year);
System.out.println("month: " + month);
sundayCount++;
}
if (currentday < 6)
currentday++;
else
currentday = 0;
}
}
}
System.out.println(sundayCount);
}
}
You forget to add into the code to check for if febuary 1900 should have 29 or 28 days. As 1900 wasn't a leap year it shouldn't have 29 days in 1900.
else if (month == 2 && year % 4 == 0 && year != 1900)
dayLimit = 29;
// February not leap year
else if (month == 2)
dayLimit = 28;
This should fix it.

Categories

Resources