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.

LAB: Praising dates
Complete main() to read dates from input, one date per line. Each date's format must be as follows: March 1, 1990. 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 -1 on a line alone. Output each correct date as: 3/1/1990.
Ex: If the input is:
March 1, 1990
April 2 1995
7/15/20
December 13, 2003
-1
then the output is:
3/1/1990
12/13/2003
LAB ACTIVITYLAB 11.6.1: Parsing dates
DateParser.java
1 import java.util.Scanner;
2
3 public class DateParser {
4 public static int get MonthAs Int(String monthString) {
5 int monthInt;
6
7 // Java switch/case statement
8 switch (monthString) {
9 case "January" :
10 monthInt = 1;
11 break:
12 case "February":
13 monthInt = 2;
14 break;
15 case "March":
17 monthInt = 3; break:
18 case "April":
19 monthInt = 4;
20 break;
21 case "May":
22 monthInt = 5;
23 break;
24 case "June":
25 monthInt = 6;
26 break;
27 case "July":
28 monthInt = 7;
29 break;
30 case "August":
31 monthInt - 8:
32 break
33 case "September":
34 monthInt = 9;
35 break;
36 case "October":
37 monthInt = 10;
38 break;
39 case "November":
40 monthInt = 11;
41 break;
42 case "December":
43 monthInt = 12;
44 break;
45 default:
46 monthInt = 0;
47 }
48
49 return monthInt;
50 }
51
52 public static void main(String[] args) {
53 Scanner scnr = new Scanner(System.in);
54
55 // TODO: Read dates from input, parse the dates to find the one
56 // in the correct format, and output in m/d/yyyy format
57
58 }
59 }
60

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");

                    }

                }    

        }

}

View image sandlee09