Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Complete main ( ) to read dates from input, one date per line. Each date's format must be as follows: March 1 , 1 9
Complete main to read dates from input, one date per line. Each date's format must be as follows: March Any date not following
that format is incorrect and should be ignored. Use the substring method to parse the string and extract the date. The input ends with
on a line alone. Output each correct date as:
Ex: If the input is:
then the output is:
Use the provided getMonthAsInt method to convert a month string to an integer. If the month string is valid, an integer in the range to
inclusive is returned, otherwise is returned. Ex: getMonthAs Int February returns and getMonthAs Int returns
import java.util.Scanner;
public class DateParser
public static int getMonthAsIntString monthString
int monthInt;
Java switchcase statement
switch monthString
case "January": monthInt ; break;
case "February": monthInt ; break;
case "March": monthInt ; break;
case "April": monthInt ; break;
case "May": monthInt ; break;
case "June": monthInt ; break;
case "July": monthInt ; break;
case "August": monthInt ; break;
case "September": monthInt ; break;
case "October": monthInt ; break;
case "November": monthInt ; break;
case "December": monthInt ; break;
default: monthInt ;
return monthInt;
public static int getDayAsIntString day
String temp ;
int dayInt;
char c day.charAtdaylength;
if c
forint i ; i day.length; i
temp day.charAti;
else
temp day;
try
dayInt Integer.parseInttemptrim;
catch NumberFormatException e
dayInt ;
return dayInt;
public static void mainString args
Scanner scnr new ScannerSystemin;
String line;
String month;
String day;
String year;
int imonth;
int iday;
int iyear;
while scnrhasNextLine
line scnrnextLine;
if lineequals
break;
Scanner lineScanner new Scannerline;
if lineScannerhasNext
month lineScanner.next;
imonth getMonthAsIntmonth;
if imonth
continue;
if lineScannerhasNext
day lineScanner.next;
iday getDayAsIntday;
if iday
continue;
if lineScannerhasNext
year lineScanner.next;
iyear Integer.parseIntyeartrim;
System.out.printlnimonth iday iyear;
lineScanner.close;
scnrclose;
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