Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help completing this java program, any help is highly appreciated! Program Specification: You will read in dates in a variety of different formats, parse

Need help completing this java program, any help is highly appreciated!

Program Specification:

You will read in dates in a variety of different formats, parse the dates, and then print them out in a converted format. The dates will be entered in one line by the user, and they will be separated by the word and.

Date formats:

Your program will parse a line of dates written in three styles: day first, month first, and all-numbers.

Day first: The day of the month is first, followed by the month, followed by the year. The month string must be at least 3 letters long, and can be a mix of upper and lowercase letters. Spaces should separate the three parts. Spaces are not allowed between numbers or month names.

ex: 8 Aug 2015

19 January 1980

Month first: The month is first, followed by the day, a comma, then the year. The month may be fully spelled out or an abbreviation at least 3 letters long and be upper or lower case. A space must separate the month and day, but there may be multiple spaces between the two.

ex: March 15, 1967

Jan 17, 1990

All numbers: Month, day, and year are entered as numbers with dashes between them. There may be many spaces around the dashes or none. Spaces are not allowed between the numbers.

ex: 12-4-2008

1-8-2003

Requirements

Your program must read a single line that may contain many dates in many formats separated by the word and. It will parse out the dates, and print them all in the correct format. The correct format is day first, a space, followed by the full name of the month with the first letter capitalized, a space, and then the year.

Your program must give an error message if a date is invalid. It should give a specific error message for the following cases:

o A month name or abbreviation is misspelled or too short (less than three characters)

o A day or month number is incorrect. Months must be between 1 and 12. Days must be valid for the month (ignore leap years)

o A year number is too low or high (1900 to 2019).

o There are too many dashes in All Numbers format.

Implementation

There are some specific requirements for how you write the program.

Use the below build-in methods

o String class: substring, trim, split, toLowerCase, indexOf, or lasIndexOf

o Integer class: parseInt

In the main() method, you must prompt the user to enter the dates, read the line of dates, break the line into separate Strings based on the delimiter (and), and for each date print Date : . You must also call the appropriate parser here.

One method for parsing each kind of date. These methods either output an error message or the corresponding standard date string.

o public static void parseDayFirst(String dateStr)

o public static void parseMonthFirst(String dateStr)

o public static void parseAllNumbers(String dateStr)

public static boolean isValidMonthDay(int day, int month)

o returns true if the month and day numbers form a valid day of the year (leap years excluded)

public static int monthToNumber(String monthStr)

o Takes a string that should be the name or abbreviation of the month name and returns the number of the month. It returns zero if the name doesnt match any month or is too short. This method is provided for you.

public static boolean isValidMonthAbbr(String month)

Takes a month as a String and returns boolean value. True means the passed in month string represents a valid month string (the length of the trimmed month string is greater than or equals 3, and the month string abbreviation is correctly spelled!!!). Hint: indexOf along with the provided static String array fullNameMonths declared at the top of the class may be useful here for.

public static boolean isValidYear(int year)

o returns true if the year is valid (1900 to 2018)

public static String standardDateString(int day, int month, int year)

o takes a day, month, and year as numbers and returns a String containing the date in the correct format, Day Month Year. (ex. 13 March 2006)

Sample Output

Date Converter!

Enter line of dates:8 Aug 2015 and March 15, 1967 and 12-4-2008

Date 1: 8 August 2015

Date 2: 15 March 1967

Date 3: 4 December 2008

Goodbye!

Date Converter!

Enter line of dates:15 xyz 2000 and 2-2-22222-and J 10, 2004 andFeb 45 , 2012

Date 1: ERROR: Invalid month string

Date 2: ERROR: Too many dashes

Date 3: ERROR: Invalid month string

Date 4: ERROR: Invalid month or day number

Goodbye!

CODE, areas that need to be completed are marked with HERE:

import java.util.Scanner; public class DateConversion { //Static variable you can use throughout this class //contains the months in shorter form private static String[] abbrMonths = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" }; //Another static variable you can use, except these contain the months fully spelled out private static String[] fullNameMonths = { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" }; public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); System.out.println("Welcome to the CS251 Date Converter! "); System.out.print("Enter line of dates:"); String dateEntries = stdIn.nextLine(); //HERE System.out.println(" Goodbye!"); stdIn.close(); } public static void parseDayFirst(String dateStr) { //HERE } public static void parseMonthFirst(String dateStr) { //HERE } public static void parseAllNumbers(String dateStr) { //HERE } public static boolean isValidMonthDay(int day, int month) { //HERE } public static int monthToNumber(String monthStr) { String lowerCaseMonthStr = monthStr.toLowerCase().substring(0, 3); int mNumber = 0; for(int i = 0; i < abbrMonths.length; i++){ if(abbrMonths[i].equals(lowerCaseMonthStr)){ mNumber = i + 1; break; } } return mNumber; } public static boolean isValidMonthAbbr(String str) { //HERE } public static boolean isValidYear(int year) { //HERE } public static String standardDateString(int d, int m, int y) { //HERE } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Autonomous Database In Enterprise Architecture

Authors: Bal Mukund Sharma, Krishnakumar KM, Rashmi Panda

1st Edition

1801072248, 978-1801072243

More Books

Students also viewed these Databases questions

Question

Simplify each root. (-10) 2

Answered: 1 week ago

Question

2. List your top 10 film villains.

Answered: 1 week ago