-3

I already know how to check for a leap year, like this:

import java.util.*;

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

            if ((year % 4 == 0) && year % 100 != 0) {
                System.out.println(year + " is a leap year.");
            } else if ((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.");
            }
        }
    }
}

But now I want to repeat this code. I've seen repeating code snippets before, and I can use just about any successfully, but this one is giving me trouble.

import java.util.Scanner;

public class LeapUpgrade
{

    public static void main(String[] args)

    {
        String another = "y";
        int year;

        Scanner scan = new Scanner(System.in);

        while (another.equalsIgnoreCase("y")) 
        {
            System.out.println("Enter year: ");
            year = scan.nextInt();

            if ((year % 4 == 0) && year % 100 != 0) 
            {

                System.out.println(year + " is a leap year.");
                System.out.print("test another (y/n)? ");
                another = scan.nextLine();

            }

            else if ((year % 4 == 0) && (year % 100 == 0)
                    && (year % 400 == 0)) 
            {

                System.out.println(year + " is a leap year.");
                System.out.print("test another (y/n)? ");
                another = scan.nextLine();

            } 

            else 

            {

                System.out.println(year + " is not a leap year.");
                System.out.print("test another (y/n)? ");
                another = scan.nextLine();
            }

        }
    }
}

What am I doing wrong here? Thanks in advance for your help and don't judge.

ayanokouji
  • 634
  • 9
  • 24
  • 4
    Can you please also provide the problem you are facing? – npinti Jan 29 '16 at 16:34
  • I need to check an integer to see if it is a leap year, which I can successfully do already, and then I need to do that as many times as the user wants. –  Jan 29 '16 at 16:36
  • Just close it since its a off-topic.. and its [Duplicate](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – SatyaTNV Jan 29 '16 at 16:42

2 Answers2

4

To avoid repetition, just encapsulate the lines of code in a method

public static boolean isLeapYear(int year){
    return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0);
}

You can then call it like this

if(LeapYear.isLeapYear(year)){
 System.out.println("Leap year");
}else{
 System.out.println("Not a leap year");
}
Rohan Kamat
  • 239
  • 1
  • 6
1

EDIT: See Rohan's answer. It's better for the sake of modularity and cleanliness/readability of code.


When you want to do something multiple times you typically just need a loop. As a hint, your loop should enclose this part of your code:

year = scan.nextInt();

if ((year % 4 == 0) && year % 100 != 0) {
    System.out.println(year + " is a leap year.");
} else if ((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.");
}

I'll leave the type of loop and conditions to you.

ZSButcher
  • 50
  • 9
  • Thank you for providing an answer. I will now proceed to take useful information and use it. And who knows, maybe someone else might benefit from this answer. – Caleb Woodman Jan 29 '16 at 16:45
  • I just feel like every time I post I need to wipe away all evidence that I did. Don't know how anybody gets any upvotes here, what with so many trolls, and it hurts my rep for when I want to help people. – Caleb Woodman Jan 29 '16 at 16:50