-3

I am making a scanner object to get a year and test to see if it is a leap year. Just want some feedback. Is this right what I have? Thank you!

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

    Scanner input = new Scanner(System.in);

    int testDate = input.nextInt();

    boolean divFour = (((testDate % 4) == 0));
    boolean divHundred = (((testDate % 100) != 0));
    boolean divFourHundred = (((testDate % 400) != 0));

    if (divFour && divHundred && divFourHundred) {
      System.out.println(testDate + " is a leap year.");
    } else {
      System.out.println(testDate + " is not a leap year.");
    }
  }
}
apxcode
  • 7,696
  • 7
  • 30
  • 41
Rafael
  • 7,605
  • 13
  • 31
  • 46
  • Can you add the exact error you are getting – Ram Oct 13 '14 at 00:45
  • Just call [`java.time.Year.isLeap`](http://docs.oracle.com/javase/8/docs/api/java/time/Year.html#isLeap-long-). – Chris Martin Oct 13 '14 at 00:52
  • Chris, this is very clearly something to learn. Of course there are built in methods to do such things, but then Rafael wouldn't learn from solving the problem. As for you, Srikanth, read Rafael's post - he is asking for simple feedback, not to correct an error. – Alex K Oct 13 '14 at 00:58

2 Answers2

0

The logic is slightly wrong. 2000 is a leap year, yet it isn't according to your program. The proper logic should've been:

if ((divFour && divHundred)||!divFourHundred) {

Also, as Dave Galvin suggested, do use better variable names to improve readability.

Vineet
  • 897
  • 10
  • 27
0

The trick is to put this into code:

A year is a leap year if it is divisible by 4, but century years are not leap years unless they are divisible by 400.

I am checking if a (year is a multiple of 4 AND not a multiple of 100) OR (year is multiple of 400).

 if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    System.out.println("Leap");

 else
    System.out.println("Not leap.");
apxcode
  • 7,696
  • 7
  • 30
  • 41