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.

Respuesta :

CPED

Answer:

Following is given the code as required with all necessary description as comments in it. The output is attached in last image.

Explanation:

I hope it will help you!

Ver imagen CPED
Ver imagen CPED
Ver imagen CPED
Ver imagen CPED

The program is an illustration of string manipulations.

String manipulations involve performing operations like merging and extracting strings.

The main() method of the program in Java, where comments are used to explain each line is as follows:

//This defines the main() method

  public static void main(String[] args) {

    //This creates a scanner object

     Scanner input= new Scanner(System.in);

//This creates an array list

     ArrayList<String>dates = new ArrayList<String>();

     //This declares all variables as string

     String date, month, day, year;

//The following loop is repeated for valid inputs

while (true) {

         //This gets input for the date

        date = input.nextLine();

//If input date is "-1"

        if (date.equals("-1")) {

           //This exits the loop

           break;      

        }

//This adds all dates to the array list        

        dates.add(date);  

     }

      //This iterates through the array list

     for (int i = 0; i < dates.size(); i++) {

   //The following try statement extracts the valid month, day and year from each date

        try {

                        month = dates.get(i).substring(0, dates.get(i).indexOf(" "));

            day = dates.get(i).substring(dates.get(i).indexOf(" ") + 1, dates.get(i).indexOf(","));

            year = dates.get(i).substring(dates.get(i).indexOf(",") + 2, dates.get(i).length());

           //This passes the month to the getMonthAsInt() method, and prints the date

           System.out.println(getMonthAsInt(month) + "/" + day + "/" + year);  

        }

//This catches all invalid dates

catch (Exception e) {}

         

     }

     

  }

   

Read more about similar programs at:

https://brainly.com/question/23447959