I know this task could be done in more practical ways, but the professor only want us to solve it with the "if" statement. I tested and it meets the condition for each task, but I still feel this code looks a bit awkward, I think there is an easier way to get it done. Can someone give me any advice?
import javax.swing.JOptionPane;
public class ValidDate {
public static void main(String[] args) {
// TODO Auto-generated method stub
String Smonth =
JOptionPane.showInputDialog ("Enter the Month");
int month = Integer.parseInt(Smonth);
if (month <= 0 || month > 12)
{
JOptionPane.showMessageDialog (null, " The month you entered is wrong" );
System.exit (0);
}
String Sday =
JOptionPane.showInputDialog ("Enter the Day");
int day = Integer.parseInt(Sday);
if (day <= 0 || day > 31)
{
JOptionPane.showMessageDialog (null, " The day you entered is wrong" );
System.exit (0);
}
else if (month == 4 || month == 6 || month == 9 || month == 11 && day > 30)
{
JOptionPane.showMessageDialog (null, " The day you entered is wrong because " + month + " month has only 30 days");
System.exit (0);
}
String Syear =
JOptionPane.showInputDialog ("Enter the Year");
int year = Integer.parseInt(Syear);
boolean leap = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));
if (leap == false && month == 2 && day >=29)
{
JOptionPane.showMessageDialog (null, " The Date you entered is wrong since " + year +" is not a leap year" );
System.exit (0);
}
else
JOptionPane.showMessageDialog (null, " The Date you entered is " + month + "/" + day + "/" + year);
}
}