Question
import java.util.Scanner; public class DateParser { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); System.out.println(Welcome to the CS251 Date Parser! ); System.out.print(Enter
import java.util.Scanner;
public class DateParser { public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in); System.out.println("Welcome to the CS251 Date Parser! ");
System.out.print("Enter line of dates:"); String dateEntries = stdIn.nextLine();
if (dateEntries.trim().isEmpty()) System.out.println("ERROR: Empty input line"); else { String[] dateSet = dateEntries.split("#");
for (int i = 0; i < dateSet.length; i++) { dateSet[i] = dateSet[i].trim();
System.out.print("Date " + (i + 1) + ": "); if (dateSet[i].isEmpty()) System.out.println("ERROR: No date entered"); else if (dateSet[i].indexOf(',') != -1) parseMonthFirstDate(dateSet[i]); else if (dateSet[i].indexOf('-') != -1) parseDasheDate(dateSet[i]); else parseDayFirstDate(dateSet[i]); } }
System.out.println(" Goodbye!");
stdIn.close(); }
public static void parseDasheDate(String dateStr) {
String parts[] = dateStr.split("-"); int dashCount = 0; for (char c : dateStr.toCharArray()) { if (c == '-') dashCount++; } if (dashCount > 2) { System.out.println("ERROR: Too many dashes"); return; }
int dayNumber = Integer.parseInt(parts[1].trim()); int monthNumber = Integer.parseInt(parts[0].trim()); int yearNumber = Integer.parseInt(parts[2].trim());
if (!isValidMonthDay(dayNumber, monthNumber)) { System.out.println("ERROR: Invalid month or day number"); return; } if (!isValidYear(yearNumber)) { System.out.println("ERROR: Invalid Year-too low"); return; }
System.out.println(standardDateString(dayNumber, monthNumber, yearNumber));
}
public static void parseMonthFirstDate(String dateStr) {
int spacerIndex = dateStr.indexOf(' '); int commaIndex = dateStr.indexOf(',');
if (!isValidMonthAbbreviation(dateStr.substring(0, spacerIndex).trim())) { System.out.println("ERROR: Invalid month string"); return; } int monthNumber = monthToNum(dateStr.substring(0, spacerIndex).trim()); int dayNumber = Integer.parseInt(dateStr.substring(spacerIndex + 1, commaIndex).trim()); int yearNumber = Integer.parseInt(dateStr.substring(commaIndex + 1).trim());
if (!isValidMonthDay(dayNumber, monthNumber)) { System.out.println("ERROR: Invalid month or day number"); return; } if (!isValidYear(yearNumber)) { System.out.println("ERROR: Invalid Year-too low"); return; }
System.out.println(standardDateString(dayNumber, monthNumber, yearNumber));
}
public static void parseDayFirstDate(String dateStr) {
int firstSpaceIndex = dateStr.indexOf(' '); String dayString = dateStr.substring(0, firstSpaceIndex).trim(); String monthYearString = dateStr.substring(firstSpaceIndex + 1).trim();
int lastSpaceIndex = monthYearString.indexOf(' '); String monthString = monthYearString.substring(0, lastSpaceIndex).trim(); String yearString = monthYearString.substring(lastSpaceIndex + 1).trim();
if (!isValidMonthAbbreviation(monthString)) { System.out.println("ERROR: Invalid month string"); return; }
int dayNumber = Integer.parseInt(dayString); int monthNumber = monthToNum(monthString); int yearNumber = Integer.parseInt(yearString);
if (!isValidMonthDay(dayNumber, monthNumber)) { System.out.println("ERROR: Invalid month or day number"); return; } if (!isValidYear(yearNumber)) { System.out.println("ERROR: Invalid Year-too low"); return; } System.out.println(standardDateString(dayNumber, monthNumber, yearNumber));
}
public static boolean isValidMonthAbbreviation(String str) {
if (str.length() < 3) { return false; } str = str.toLowerCase(); String[] months = { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" };
for (String month : months) { if (month.indexOf(str) == 0) { return true; } }
return false; }
public static int monthToNum(String mStr) {
String lowerCaseMStr = mStr.toLowerCase().substring(0, 3);
int mNumber = 0; switch (lowerCaseMStr) {
case "jan": mNumber = 1; break; case "feb": mNumber = 2; break; case "mar": mNumber = 3; break; case "apr": mNumber = 4; break; case "may": mNumber = 5; break; case "jun": mNumber = 6; break; case "jul": mNumber = 7; break; case "aug": mNumber = 8; break; case "sep": mNumber = 9; break; case "oct": mNumber = 10; break; case "nov": mNumber = 11; break; case "dec": mNumber = 12; break; } return mNumber;
}
public static boolean isValidMonthDay(int day, int month) {
int maxDays = 0;
switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: maxDays = 31; break;
case 4: case 6: case 9: case 11: maxDays = 30; break;
case 2: maxDays = 28; break;
}
return (maxDays != 0 && day > 0 && day <= maxDays); }
public static boolean isValidYear(int year) { return (year >= 1900); }
public static String standardDateString(int d, int m, int y) { String month;
switch (m) { case 1: month = "January"; break; case 2: month = "February"; break; case 3: month = "March"; break; case 4: month = "April"; break; case 5: month = "May"; break; case 6: month = "June"; break; case 7: month = "July"; break; case 8: month = "August"; break; case 9: month = "September"; break; case 10: month = "October"; break; case 11: month = "November"; break; case 12: month = "December"; break; default: month = "-"; }
return d + " " + month + " " + y; } }
You are required to use posted solution for assignment 2. (posted above)
This assignment is designed to give you some practice with exception handling. We are giving you our solution to Program 2, which was the date parser assignment. Our solution is of good quality, but there are other ways to make good solutions. The program has the below error messages:
ERROR: Invalid month or day number
ERROR: Invalid month string
ERROR: Invalid Year(too low)
ERROR: Too many Dashes
For this assignment, your task is to change how errors in the declarations are processed so that they no longer print messages themselves. Rather, they will throw exceptions that will be caught in main() or in other methods that you add to the program. Using this design forces some changes in how declaration errors get reported.
2 More specifically
You will write four exception classes: InvalidMonthorDayException, InvalidMonthStringException, InvalidYearException, and TooManyDashesException.
You must replace the System.out.println statements that prints the above listed errors with appropriate throw statements. In main(), you will need to add try/catch block(s) in order to handle the exceptions and print error messages.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started