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: 3795423 • 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)***

---------------

help please it must have

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

Explanation / Answer

import java.util.StringTokenizer;

import javax.swing.JOptionPane;

public class TestDate

{

public static void main(String[] args)

{

String date=JOptionPane.showInputDialog("Enter the date");

date=date.trim(); // mm/dd/yyyy or mm-dd-yyyy

check(date);

}

public static void check(String date)

{  

int a[]=new int[3];

StringTokenizer st=new StringTokenizer(date,"/-");

if(date.indexOf("/")>0 && date.indexOf("-")>0){ // mixed combo of /,- is an error

System.out.println(date+" Not Valid: use either / or - ."); return; }

else

for(int i=0;st.hasMoreTokens();i++) // storing values in array

a[i]=Integer.parseInt(st.nextToken());

if(a[0]>12 || a[0]<1) { // checking if month exceeds 12

System.out.println(date+" Not Valid: Month has to b/w 1 and 12 inclusive.");

return;

}

if(a[1]>31 || a[0]<1) { // checking if date exceeds 31

System.out.println(date+" Not Valid: date has to b/w 1 and 31 inclusive.");

return;

}

String month[]={"January","February","March","April","May","June","July","August","September","October","Novermber","December"};

boolean monthCheck= a[0]==4 || a[0]== 6|| a[0]==9 || a[0]==11;

if(monthCheck && a[1]==31){ // checking if for specific months date exceeds 30

System.out.println(date+" Not Valid: "+month[a[0]-1]+" month has only 30 days");

return;

}

boolean yea=false;

yea=a[2]%400==0 || (a[2]%4==0 && a[2]%100!=0 );

if(!yea && a[0]==2 && a[1]>28){ // not leap year, feb, date exceeds 28

System.out.println(date+" Not valid: In non leap year ("+a[2]+"), February has only 28 days"); return; }

else if (yea && a[0]==2 && a[1]>29) {// leap year, feb, date exceeds 29

System.out.println(date+" Not Valid: In leap year ("+a[2]+"), February has only 29 days"); return; }

System.out.println("Date ("+date+") is correct");

}

}

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