Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java console application that reads a string from the keyboard and tests

ID: 3862199 • Letter: W

Question

Write a Java console application that reads a string from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is NOT valid.

The input date will have the format mm/dd/yyyy.
A valid month value mm must be from 1 to 12 (January is 1). You can use:

String monthPart = inputDate.substring(0, 2);
int month = Integer.parseInt(monthPart);
to get the month value.

A valid day value dd must be from 1 to a value that is appropriate for the given month. September, April, June, and November each have 30 days. Assume February has 28 days. All other months have 31 days.
The year can be any 4 digit year.

***Be sure to use a switch statement at least once (perhaps for checking the valid months)***

Explanation / Answer

//ValidDate.java

import java.util.Scanner;

public class ValidDate {

   public static void main(String[] args) {
      
   //Array of months
   String months[] = {
       "1,January,31","2,Feburary,28", "3,March,31","4,April,30"
       ,"5,May,31","6,June,30","7,July,31","8,August,31",
       "9,September,30","10,October,31","11,November,30"
       ,"12,December,31"
   };
      
       // Reading date as String with mm/dd/yyyy
       String sDate;
       Scanner s = new Scanner(System.in);
       System.out.println("Enter the date:");
       sDate = s.next();
      
       String f[] = sDate.split("/"); // Splitting date into 3 parts
       if(f.length<3){
           System.out.println("Invalid Input... Give as mm/dd/yyyy");
       }else {
       int day = Integer.parseInt(f[1]); // Day 1-30 or 31 or 28
       int month = Integer.parseInt(f[0]); // Months 1-12
       String year = f[2]; // Year 4 digit number
       String x="";
       boolean flag = false;
       StringBuffer sb = new StringBuffer();
       switch(month){ // Passing month ino switch
       case 1:
       case 2:
       case 3:
       case 4:
       case 5:
       case 6:
       case 7:
       case 8:
       case 9:
       case 10:
       case 11:
       case 12:               
           x = months[month-1]; // getting value from array
           String dd[] = x.split(","); // split using comma
           if(day<=Integer.parseInt(dd[2])){ // Validating Day
               if(year.length()==4){ // Validating year
                   flag = true;
               } else{
                   sb.append("Invalid Year.. year should be 1000-9999");
               }
           }else {
               sb.append("Invalid Number of days for Month "+dd[1]);
           }
           break;
       default : sb.append("Invalid Month, should be between 1-12"); // validating Month
       }
      
       if(flag){
           System.out.println("Valid Date Format:");
       } else {
           System.out.println("Invalid Date format");
           System.out.println(sb.toString());
       }
   }
      
   }

}

Output:

Enter the date:
12/2016
Invalid Input... Give as mm/dd/yyyy

Enter the date:
12/12/2018
Valid Date Format:

Enter the date:
02/29/2017
Invalid Date format
Invalid Number of days for Month Feburary

Enter the date:
14/22/2016
Invalid Date format
Invalid Month, should be between 1-12

Enter the date:
12/12/201
Invalid Date format
Invalid Year.. year should be 1000-9999

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote