7
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.

peer
  • 4,171
  • 8
  • 42
  • 73
Rick Vaughn
  • 71
  • 1
  • 2
  • 6
    Think about this condition: `(year < 1582) == (year % 4==0)`. Think *very* carefully about what that is checking. I don't think it's what you mean it to be. (Hint: I'd just separate "everything before 1582" from "everything from 1582 onwards", and ignore the fact that those two branches will have anything in common...) – Jon Skeet Sep 01 '14 at 21:24
  • There is a syntax error in your code - a `;` just after the `if` statement, which finishes the `if` block without the body. The following `else` is left without any corresponding `if`. – papacito Sep 01 '14 at 21:31
  • I think the year of splitting is 1752, not 1582... can you check that ?? if you are under linux, check cal 1700 :)) I cannot advise for windows as I don't have it.. – mlwn Sep 01 '14 at 21:45
  • Can you simply use Java standard libraries like this http://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#isLeapYear(int) ? – mvp Sep 02 '14 at 00:15
  • @mvp - Yes, see the answer below from sadasidha – marklark Mar 30 '16 at 21:55
  • In Java 8 and later: [`java.time.Year.of( 2017 ).isLeap()`](https://docs.oracle.com/javase/9/docs/api/java/time/Year.html#isLeap--) – Basil Bourque Dec 31 '17 at 22:03

2 Answers2

1

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");
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

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;
}
mirmdasif
  • 6,014
  • 2
  • 22
  • 28