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

Write a program that reads a string from the keyboard and tests whether it conta

ID: 3583878 • Letter: W

Question

Write a program 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). The day value dd must be from 1 to a value that is appropriate for
the given month. February has 28 days except for leap years when it has 29. A leap
year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by
400.

https://www.chegg.com/homework-help/java-7th-edition-7th-edition-chapter-3-problem-4prop-solution-9780133834611

if i want to use this SOULTION as my assignment quesiton, may i know how to edit OR change the codes

Explanation / Answer


public class InvalidDetailsException extends Exception {
   public String error;

   public InvalidDetailsException(String error) {
      
       this.error=error;
   }
  
  

}

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

import java.util.*;

public class DateValidation

{
public static void main(String[] args)
{   
System.out.print("please enter the date this format only mm/dd/yyyy:");
final Scanner in = new Scanner(System.in);
String date=in.next();//reading data from console
String datemonthyear[]=date.split("/");
if(datemonthyear.length!=3||datemonthyear[0].length()!=2||datemonthyear[1].length()!=2||datemonthyear[2].length()!=4)
{
main(args);
return;
}
int leap=0;
int days=0;
int month=0;
String error="";
try
{
for(int i=datemonthyear.length-1;i>=0;i--)
{
int temporary=new Integer(datemonthyear[i]);
if(i==2)
{
if((temporary%4==0&&temporary%100!=0)||temporary%400==0)//check leap year
{
leap=1;
}
}
if(i==1)
{
if(temporary>12)
{
error="your given month value is greater than 12";
throw new InvalidDetailsException(error);
}
if(temporary==4||temporary==6||temporary==9||temporary==11)
{
days=30;
month=temporary;
}
else if(temporary==2)
{
if(leap==1)
{
days=29;
}
else
{
days=28;
}
month=temporary;
}
else//computing days in a month
{
days=31;
month=temporary;
}

}
if(i==0)
{
if(temporary>days)//formating error string
{
error="the actual days containong the month "+month+" is"+days+" but your given days contains "+temporary+" days";
throw new NumberFormatException();
}
}
}
}
catch(InvalidDetailsException e)
{
System.out.println(error);
main(args);
return;
}
System.out.println("your given date is "+date+" valid");
}
}

output

please enter the date this format only mm/dd/yyyy:02/05/2016
your given date is 02/05/2016 valid

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