Leap Year Java Program






This program confirms if the entered year is a leap year or not.

...................................................................................................................................................................


import javax.swing.JOptionPane;

public class LeapYear {
public static void main(String args[]){


int Year=0;
boolean goodInput = false;


//JOptionPane.showMessageDialog(null,
// "This program shows whether the year you are about " +
//"to enter is a leap year or not . ");


String YearString=JOptionPane.showInputDialog(null,"Enter the year :");


do
   {
     try
     {
       // attempt to convert the String to an integer
       Year = Integer.parseInt( YearString );
       
       goodInput = true;
   
     }
     catch (NumberFormatException nfe)
     {
       YearString = JOptionPane.showInputDialog( null,"'"+YearString+"'" + " is an invalid Year, "+ "Enter the Correct Year.","Invalid Input !",JOptionPane.ERROR_MESSAGE);
     
   
     }
   } while ( !goodInput );



if (Year>3000){
JOptionPane.showMessageDialog(null,"OK now look, its 2012, let's not be funny. Input at least the year that is reasonable."+
"\n "+Year+ " is way too much, put less than 3000.");
System.exit(Year);
}

boolean isLeapYear= (Year % 4 == 0 && Year % 100 !=0) || (Year % 400==0);

JOptionPane.showMessageDialog(null,
"Is the year "+ Year+ " a Leap Year or Not?"+
"\nPress Return (Enter) or  Click OK for the answer." );

if (isLeapYear==true){
JOptionPane.showMessageDialog(null,
Year+" is a Leap Year!");
}
else  {
JOptionPane.showMessageDialog(null,
Year+" is not a Leap Year!"+
"\nDid you know that leap year only comes once in 4 years?");

}
}



}


Comments

Popular posts from this blog