Can someone please help me find the error in my program here? It is
for determining a user input year as a leap year or not using the
Gregorian calendar. I'm trying to do it by using if statements. When
I enter 2004, a leap year, it doesn't work. Thanks much.
Here's the code:
import java.util.Scanner;
public class LeapYear
{
public static void main (String[] args)
{
final int YEAR_MIN = 1582;
Scanner scan = new Scanner (System.in);
System.out.print ("Enter potential leap year: ");
int year = scan.nextInt();
if (year > YEAR_MIN)
{
if (year%4 == 0)
{
if (!((year%100 == 0) && !(year%400 == 0)))
{
System.out.print ("Not Leap. ");
}
else
System.out.println ("Leap Year. ");
}
else
System.out.println ("Not Leap Year. ");
}
else
System.out.println ("Before Gregorian Calendar.");
}
}
Rhino - 16 Mar 2005 22:34 GMT
> Can someone please help me find the error in my program here? It is
> for determining a user input year as a leap year or not using the
[quoted text clipped - 44 lines]
> }
> }
Have you tried stepping through your code in a debugger? I've always found
that to be the best way to learn what is wrong. It saves you from having to
write posts and wait for answers and you usually learn some things as you
trace through your code.
You do have a debugger right? If not, I'd recommend getting one if you have
any aspirations of being a professional developer or of understanding Java.
I'm quite fond of the debugger in Eclipse but I'm sure there are many others
out there.
You haven't said why you think there is an error in your code. Are you
getting a compile error? Are you getting a runtime error? Or is it just not
giving the right answer to some of your test cases?
Rhino
hdquimby@yahoo.com - 16 Mar 2005 23:09 GMT
It compiles fine, I get the error when running the program. For
example, at the input prompt I type 2004, I get the message "Not Leap."
When I type a year that's not divisible by 4, I get the message "Not
Leap Year." indicating there's an error somewhere in the 3rd if
statement I believe.
I created the 2 "Not leap" statements to find where the error may be.
No year I have typed has given me the "Leap Year." response. I know
the (year > YEAR_MIN) works. Try running the program perhaps you'll
see what I mean.
Thanks so far Rhino
hdquimby@yahoo.com - 16 Mar 2005 22:42 GMT
Sorry forgot to say I know my problem is in this part, it's either the
if/else structure or the if statement of year divisibilty by 100 & 400
> if (year%4 == 0)
>
[quoted text clipped - 11 lines]
>
> System.out.println ("Not Leap Year. ");
Bjorn Abelli - 16 Mar 2005 23:28 GMT
<hdquimby@yahoo.com> wrote...
> Can someone please help me find the error in my program here? It is
> for determining a user input year as a leap year or not using the
> Gregorian calendar. I'm trying to do it by using if statements. When
> I enter 2004, a leap year, it doesn't work. Thanks much.
It's in this line:
> if (!((year%100 == 0) && !(year%400 == 0)))
The *inner* condition satisfies the Gregorian rule, but then you negate
it...
// Bjorn A
Mark Haase - 16 Mar 2005 23:39 GMT
> if (!((year%100 == 0) && !(year%400 == 0)))
The problem is, indeed, right here.
(year % 100 == 0) && !(year % 400 == 0)
When you put in 2004, the above expression results in false. This means
your code returns true...and so it prints out "Not Leap". You could do
this by breaking down the if statements more logically:
boolean isLeapYear(int year) {
if (year % 400 == 0) // it IS a leap year
return true;
else if ((year % 4 == 0) && !(year % 100 == 0)) // it IS a leap year
return true;
else // if you get here, it is NOT a leap year
return false;
}
Notice that the "else" is optional and the "if" and "else if" could be
combined as follows:
boolean isLeapYear(int year) {
if ( (year % 400 == 0) || ((year % 4 == 0) && !(year % 100 == 0)) )
return true;
return false;
}
but this code is harder to read...
|\/| /| |2 |<
mehaase(at)sas(dot)upenn(dot)edu
John - 17 Mar 2005 06:44 GMT
>> if (!((year%100 == 0) && !(year%400 == 0)))
>
[quoted text clipped - 27 lines]
>
> but this code is harder to read...
Not looking as nice but you can adjust that problem:
For GregorianCalendar:
public static final boolean isLeapViaPopeGregory ( int yyyy)
{
if ( yyyy < 0 ) return( yyyy + 1 ) % 4 == 0;
if ( yyyy < 1582 ) return yyyy % 4 == 0;
if ( yyyy % 4 != 0 ) return false;
if ( yyyy % 100 != 0 ) return true;
if ( yyyy % 400 != 0 ) return false;
return true;
}
For Julian Calendar:
public static final boolean isLeapViaJulian ( int yyyy)
{
if ( yyyy < 0 )
{
return( yyyy + 1 ) % 4 == 0;
}
else
{
return yyyy % 4 == 0;
}
}