# Thread: Leap Year Test: Help!

1. Member
Join Date
Apr 2012
Posts
74
Rep Power
0

## Leap Year Test: Help!

The Challenge is:

Write a program that will read in a year and report if it is a leap year. The tricky thing here is that a leap year occurs:

on every year that is evenly divisible by 4

except every year that is evenly divisible by 100

except every year that is evenly divisible by 400.

My code is:

import java.util.Scanner;
public class LeapYear {
public static void main(String args[]){

System.out.println("Enter Year: ");
Scanner year = new Scanner(System.in);
int num;
num = year.nextInt();

if (num % 4 == 0 && num % 100 != 0 && num % 400 != 0){
System.out.println("Leap Year!");
}
else{
System.out.println("Not a Leap Year!");
}

}
}

But if I input 2000, which is supposed to be a leap year, it returns the else statement. I don't understand why it's not working?

2. Member
Join Date
Apr 2012
Posts
74
Rep Power
0

## Re: Leap Year Test: Help!

Ok I googled, and the challenge was worded badly. Every year evenly divisible by 400 IS a leap year, which makes it more complicated. So from an example I came up with this code:

import java.util.Scanner;
public class LeapYear {
public static void main(String args[]){
System.out.println("Enter Year: ");
Scanner year = new Scanner(System.in);
int num;
num = year.nextInt();

if (num % 4 == 0)
{
if (num % 100 == 0)
{
if (num % 400 != 0)
{
System.out.println("Not a Leap Year.");
}
if (num % 400 == 0){
System.out.println("Leap Year!");
}
}
if (num % 100!= 0)
{
System.out.println("Leap Year!");
}
}
if (num % 4 != 0){
System.out.println("Not a Leap Year.");
}
}
}

Which makes sense to me, and it works! :D

But now I want it to clear the screen, and "reset" so you can input another year without rerunning the app. I've googled for solutions, but didn't really find any. Any know how to do that?

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

## Re: Leap Year Test: Help!

But if I input 2000, which is supposed to be a leap year, it returns the else statement.
Strictly speaking it doesn't return the else statement, it follows the else branch and executes the statement printing "Not a Leap Year!". And that's because 2000%100 is zero. (no remainder). Hence num%100!=0 is false. Hence ...&&num%100!=0&&... is false. Hence the else branch is followed.

Wikipedia begins with a brief description of what years are counted as "leap", if you are unsure. You might avoid a tangle of boolean operators if you don't try and cram the whole test within a single if statement. You can nest them:

Java Code:
```if(whatever) {
if(whatever-else) {
// A
} else {
// B
}
} else {
if(some-third-thing) {
// C
} else {
// D
}
}```
Maybe not that complex, but you get the idea. Try and model with if statements the kind of explanation you might give to a small child.

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

## Re: Leap Year Test: Help!

Slow :( But glad you've got something that yields the right answer. (When posting code use the "code" tags: put [code] at the start of the code and [/code] at the end. That way the all important indents will be visible.)

-----

Java doesn't really do "clearscreen". Or have any nice console methods. For this you need to write a graphical application.

5. Member
Join Date
Apr 2012
Posts
74
Rep Power
0

## Re: Leap Year Test: Help!

Thanks pbrockway2!

Testing code tag:

Java Code:
```import java.util.Scanner;
public class LeapYear {
public static void main(String args[]){
System.out.println("Enter Year: ");
Scanner year = new Scanner(System.in);
int num;
num = year.nextInt();

if (num % 4 == 0)
{
if (num % 100 == 0)
{
if (num % 400 != 0)
{
System.out.println("Not a Leap Year.");
}
if (num % 400 == 0){
System.out.println("Leap Year!");
}
}
if (num % 100!= 0)
{
System.out.println("Leap Year!");
}
}
if (num % 4 != 0){
System.out.println("Not a Leap Year.");
}
}
}```
Ok I think I'll leave the GUI for now (not up to it in my tutorials) and just move on to the next challenge.

Know any good resources for short java coding challenges for beginners?

Cheers!

PS. How do you get the [code] tag to keep the line numbers?

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

## Re: Leap Year Test: Help!

How do you get the [code] tag to keep the line numbers?
The line numbers are inserted by some CSS magic, so you don't have control over them. (They can take a few seconds to load, at least on my machine...)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•