'Java finding the next leap year
How do I find the next leap year if the given input isn't a leap year?
A year is considered a leap year when it is either:
- divisible by 4, but not 100 or
- divisible by both 4, 100, and 400 at the same time
Input:
A single line containing a number that represents a year, here 1702.
Output:
The next soonest leap year if the input is not a leap year. Otherwise, output "Leap year".
The next leap year is 1704
Heres my code: When I input 1702 nothing shows but if it's 1700 it works. If ever you can help pls only use if else or while since these two are the only allowed for it to run.
import java.util.Scanner;
class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int year = input.nextInt();
int leap = 0;
if (year % 400 == 0) {
System.out.print("Leap year");
} else if (year % 100 == 0) {
leap += 4;
year += leap;
System.out.print("The next leap year is ");
System.out.print(year);
} else if (year % 4 == 0) {
System.out.print("Leap year");
}
input.close();
}
}
Solution 1:[1]
tl;dr short solution with java.time:
Java 8+ provide the package java.time which has a class Year which provides a method to determine if that very year was/is/will be a leap year, that is Year.isLeap().
You could use it to get your desired result, maybe like in the following example:
public static void main(String[] args) {
// hard-coded example number
int numericalYear = 1702;
// create the year from the number given
Year year = Year.of(numericalYear);
// check if that year is leap
if (year.isLeap()) {
System.out.println("Leap Year");
} else {
// of find the next one by adding 1 year and checking again
while (!year.isLeap()) year = year.plusYears(1);
// print the next one
System.out.println("Next leap year is " + year);
}
}
This example has an output of
Next leap year is 1704
Solution 2:[2]
The major part of the main() is an if-statement with three branches. I think the most difficulties you have is you mixed the part to determine a leap year, and the part to control the workflow.
The workflow of your program is:
- Take an user input
- Check if this is a leap year
- Print the response according to step 2
The logic to determine a leap year is:
- divisible by 4, but not 100 or
- divisible by both 4, 100, and 400 at the same time
You could try to isolate these two area with two different piece of code. Take one step at a time. Then things will become less complex. You can simplify your if from three to two branches. Since the year is either a leap or not, no other possibility. If it is leap year, print the year. Else, print next leap year. Only two cases.
In order to check whether a year leap year or not, obviously you need an algorithms. You can decouple the algorithms into a separate function. This way you can isolate the leap year logic with your workflow.
Example
import java.util.Scanner;
public class Main{
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int year = input.nextInt();
// base cycle is 4 year
final int leapCycle = 4;
if (isLeapYear(year)) {
System.out.print("Leap year");
} else {
// complete the year with next cycle
year += leapCycle - (year % leapCycle);
// the next maybe not a leap year e.g. 1800, 1900
if (!isLeapYear(year)) {
// then advance it with one more cycle
year += leapCycle;
}
System.out.print("The next leap year is ");
System.out.print(year);
}
input.close();
}
private static boolean isLeapYear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
return year % 400 == 0;
} else {
return true;
}
}
return false;
}
}
Test cases
1700
The next leap year is 1704
1702
The next leap year is 1704
1799
The next leap year is 1804
1800
The next leap year is 1804
1999
The next leap year is 2000
2000
Leap year
2001
The next leap year is 2004
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | deHaar |
| Solution 2 | Mr. Brickowski |
