Write an application in which the user can enter a date using digits and slashes
ID: 3622528 • Letter: W
Question
Write an application in which the user can enter a date using digits and slashes( for example "2/4/2010"), and receive output that displays the date with the month shown as a word( such as February 4, 2010"). Allow for the fact that the user might or might not precede a month or a day number with a zero( for example, the user might type "02/04/2010" or "2/4/2010"). Do not allow the user to enter an invalid date, defined as one for which the month is less than 1 or more then 12, or one for which the day number is less then 1 or greater than the number of days in the specified month. Also display the date's ordinal position in the year; for example, 2/4 is the 35th day. In this application, use your knowledge of arrays to store the month names, as well as values for the number of days in each month so that you can calculate the number of days that have passed. Save the application as ConvertDate.java.Explanation / Answer
package convertdate; import java.util.Scanner; import java.util.regex.MatchResult; import javax.swing.JOptionPane; public class ConvertDate { static final int MONTH = 0; static final int DAY = 1; static final int YEAR = 2; static final String[] monthName = { "UNUSED", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; static int[] daysInMonth = { -1, // UNUSED 31, // January 28, // February 31, // March 30, // April 31, // May 30, // June 31, // July 31, // August 30, // September 31, // October 30, // November 31, // December }; // // returns an array of 3 ints {day, month, year) if valid // returns null if invalid // static int[] parseDate (String input) { int[] ret = new int[3]; // // the scanner will look for 3 numbers separated by slashes // Scanner scanner = new Scanner(input); scanner.findInLine("(\d+)/(\d+)/(\d+)"); try { MatchResult result = scanner.match(); ret [MONTH] = Integer.parseInt(result.group(1)); ret [DAY] = Integer.parseInt(result.group(2)); ret [YEAR] = Integer.parseInt(result.group(3)); return ret; } catch (Exception ex) { return null; } } /** * @param args the command line arguments */ public static void main(String[] args) { String response; int retdate [] = null; while (true) { response = JOptionPane.showInputDialog(null, "Enter date:"); if (response == null) System.exit(0); retdate = parseDate(response); if (retdate == null) continue; if (retdate[YEAR] < 0) continue; // adjust the days per month for leap years if (retdate[YEAR] % 4 == 0) { if (retdate[YEAR] % 100 == 0) { if (retdate[YEAR] % 400 == 0) daysInMonth[2] = 29; else daysInMonth[2] = 28; } else daysInMonth[2] = 29; } else daysInMonth[2] = 28; // check the month if (retdate[MONTH] < 1 || retdate[MONTH] > 12) continue; // check the day if (retdate[DAY] < 1 || retdate[DAY] > daysInMonth[retdate[MONTH]]) continue; break; } String result1 = "Date is " + monthName[retdate[MONTH]] + " " + retdate[DAY] + ", " + retdate[YEAR] + " "; int dayInYear = 0; for (int i = 1; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.