Westonci.ca offers fast, accurate answers to your questions. Join our community and get the insights you need now. Get quick and reliable answers to your questions from a dedicated community of professionals on our platform. Explore comprehensive solutions to your questions from knowledgeable professionals across various fields on our platform.
Sagot :
Answer:
Explanation:
The following is the entire modified code, it takes in the date as an input and continues changing the dates to the desired format. If the input format is wrong it says "Wrong Format" and requests another input. If -1 is passed as an input the program terminates.
import java.util.Scanner;
class DateParser {
public static int getMonthAsInt(String monthString) {
int monthInt;
switch (monthString) {
case "January" :
monthInt = 1;
break;
case "February":
monthInt = 2;
break;
case "March":
monthInt = 3;
break;
case "April":
monthInt = 4;
break;
case "May":
monthInt = 5;
break;
case "June":
monthInt = 6;
break;
case "July":
monthInt = 7;
break;
case "August":
monthInt = 8;
break;
case "September":
monthInt = 9;
break;
case "October":
monthInt = 10;
break;
case "November":
monthInt = 11;
break;
case "December":
monthInt = 12;
break;
default:
monthInt = 0;
}
return monthInt;
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
// TODO: Read dates from input, parse the dates to find the one
// in the correct format, and output in m/d/yyyy format
String date = "";
while (!date.equals("-1")) {
String month = "";
String day = "";
String year = "";
System.out.println("Enter date:");
date = scnr.nextLine();
try {
year = date.substring((date.length()-4), date.length());
if (Character.isDigit(date.charAt(date.length()-8))) {
day = date.substring((date.length()-8), date.length()-6);
month = date.substring(0, date.length()-9);
} else {
day = date.substring((date.length()-7), date.length()-6);
month = date.substring(0, date.length()-8);
}
if ((year.length() == 4) && (day.length() > 0) && (day.length() <= 2) && (month.length() >= 3)) {
System.out.println(getMonthAsInt(month) + "/" + day + "/" + year);
} else {
System.out.println("Wrong Format");
}
} catch (Exception e) {
System.out.println("Wrong Format");
}
}
}
}
Thanks for stopping by. We are committed to providing the best answers for all your questions. See you again soon. Thank you for your visit. We're dedicated to helping you find the information you need, whenever you need it. Westonci.ca is committed to providing accurate answers. Come back soon for more trustworthy information.