I'm fairly new to Java and I'm trying to make a program that allows me to enter a month, then enter a day. I want it to end if the day within the month is valid (I.e. 28 in February is valid, 29 is invalid). I've made it work, but I've used 12 IF statements for each month, and it looks too messy and like too much code. Could anyone suggests a better method of condensing this down?
import java.util.Scanner;
public class Calender {
public static void main(String[] args) {
int month=0;
int day=0;
Scanner in = new Scanner (System.in);
do {
System.out.print("Enter a month [1..12]: ");
month=in.nextInt();
//continue;
} while (month > 12);
//System.out.println ("This is not a valid month.");
do {
System.out.print("Enter a day [1..31]: ");
day=in.nextInt();
} while (day > 31);
if (day > 31 && month == 1) {
System.out.print("This is not a valid day of the month");
}
if (day > 31 && month == 3) {
System.out.print("This is not a valid day of the month");
}
if (day > 31 && month == 5) {
System.out.print("This is not a valid day of the month");
}
if (day > 31 && month == 7) {
System.out.print("This is not a valid day of the month");
}
if (day > 31 && month == 8) {
System.out.print("This is not a valid day of the month");
}
if (day > 31 && month == 10) {
System.out.print("This is not a valid day of the month");
}
if (day > 31 && month == 12) {
System.out.print("This is not a valid day of the month");
}
if (day > 30 && month == 4) {
System.out.print("This is not a valid day of the month");
}
if (day > 30 && month == 6) {
System.out.print("This is not a valid day of the month");
}
if (day > 30 && month == 9) {
System.out.print("This is not a valid day of the month");
}
if (day > 30 && month == 11) {
System.out.print("This is not a valid day of the month");
}
if (day > 28 && month == 2) {
System.out.print("This is not a valid day of the month");
}
}
}