/** * This programs reads an integer to indicate a month. * It prints the number of days in the month and the name * of the month. */ import javax.swing.JOptionPane; public class PrintMonthDemo { public static void main( String[] args ) { String input = JOptionPane.showInputDialog( "Enter an integer between 1 and 12:" ); int monthNum = Integer.parseInt( input ); if( monthNum < 1 || monthNum > 12 ) { // Invalid: not in range. JOptionPane.showMessageDialog(null, "Integer must be between 1 and 12" ); System.exit(0); } // Determine the number of days in the month int numDaysInMonth; switch( monthNum ) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDaysInMonth = 31; break; case 4: case 6: case 9: case 11: numDaysInMonth = 30; break; case 2: numDaysInMonth = 28; // ignore leap years for now break; default: numDaysInMonth = -1; break; } // Determine the month name. String monthName; switch( monthNum ) { case 1: monthName = "January"; break; case 2: monthName = "February"; break; case 3: monthName = "March"; break; case 4: monthName = "April"; break; case 5: monthName = "May"; break; case 6: monthName = "June"; break; case 7: monthName = "July"; break; case 8: monthName = "August"; break; case 9: monthName = "September"; break; case 10: monthName = "October"; break; case 11: monthName = "November"; break; case 12: monthName = "December"; break; default: monthName = "???"; break; } String output = monthName + " has " + numDaysInMonth + " days."; JOptionPane.showMessageDialog( null, output ); System.exit(0); } }