-1

I need to write a method for a leap year. So the method has to be called printleap and checks to see if the number is a leap year or not. So the the java program will test the method by reading a list of year number and only displays the leap years. Here is what I have done:

import java.util.Scanner; public class Ex1PartAassig3 {

public static int printleap(String string) {
    Scanner sc1=new Scanner(System.in);
    {
        System.out.println("This programe calculates leap year.");
        int year= printleap ("Enter the year:");
        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.");

    }

}

}

The areas that are showing a mistake are underline the first 'else' else if ((year % 4==0) && (year % 100==0)&&(year % 400==0)

and two curly brackets, the first just above the second system.out and the 1st bracket at the very bottom.There is 3 brackets on the bottom but its the first 1.

Could some help me to run this program or what I have forgotten thank you?

Ciaran
  • 1
  • 1
    After your first `if` statement, you immediately have an errant closing bracket. Take some time to look at where your brackets are opening and closing. – LiXie Nov 13 '16 at 21:03
  • 1
    Doesn't this look suspicious to you? `if ((year%4==0)&& year % 100 !=0) }` – Marvin Nov 13 '16 at 21:04
  • 1
    Your printleap() method calls your printleap() method. That is unnecessary. Don't bother about asking the user to enter a year. Start by writing the method you're being asked to write: a printleap() method, that takes a year as argument (so, an int, not a String), and which prints if that year is a leap year or not. Dealing with user input, if you have to do it, should be done in a separate method. You can first test the printleap() method by calling it with hard-coded years. – JB Nizet Nov 13 '16 at 21:07

4 Answers4

3

Your curly braces are all over the place and don't line up. This would be a fixed version with properly matched braces:

public static void printleap()
{
    try (Scanner sc1=new Scanner(System.in))
    {
        System.out.println("This programe calculates leap year.");
        System.out.print("Enter the year:");
        int year= sc1.nextInt();
        if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
        {
            System.out.println(year +"is a leap year.");
        }
        else
        {
            System.out.println(year + " is not a leap year.");
        }
    }
}

EDIT: Also updated the if-statement to use the condition of the answer below, since the one in the initial code is indeed flawed. A leap year must be evenly divisible by 4, but a year that is evenly divisible by 100 is only a leap year if it's also evenly divisible by 400.

Crusha K. Rool
  • 1,502
  • 15
  • 24
1

your condition is wrong...

it must be

 if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • No, this is one of the things about the original program that ISN'T wrong. The OP has simply broken this condition down into three separate cases. But their logic for ascertaining which years are leap years is actually correct. – Dawood ibn Kareem Nov 13 '16 at 21:35
  • Thanks for your comment: – Ciaran Nov 13 '16 at 21:35
  • public static int printleap(String string) { Scanner sc1=new Scanner(System.in); { System.out.println("This programe calculates leap year."); int year= printleap ("Enter the year:"); if((year % 400 == 0) && ((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."); } return 0; } } – Ciaran Nov 13 '16 at 21:37
  • Sorry i tried to show what i was getting but it doesnt seem to be coming out correctly – Ciaran Nov 13 '16 at 21:38
1

Your logic (uses if and else if) should be as follows:

if ( year%400 == 0)
    System.out.println(year+ " is leap year");
  else if ( year%100 == 0)
    System.out.println(year+ " is NOT leap year");
  else if ( year%4 == 0 )
    System.out.println(year+ " is leap year");
  else
     System.out.println(year+ " is NOT leap year");
Vasu
  • 21,832
  • 11
  • 51
  • 67
0

Using java.time

There's a class for that.

Year.now()
    .isLeap()

Better to always specify your desired/expected time zone rather than rely implicitly on the JVM’s current default zone.

Year.now( ZoneId.of( "Europe/Paris" ) )
    .isLeap()

Or specify a year.

Year.of( 2017 )
    .isLeap()

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154