Write a program that reads a string from the keyboard and tests whether it conta
ID: 671795 • 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.
My coding is below. but the format is in DD/MM/YY. I would like to change it to MM/DD/YYYY
import java.util.Scanner;
public class Q1
{
public static void main(String[] args)
{
System.out.print("enter date containing format mm/dd/yyyy:");
final Scanner in = new Scanner(System.in);
String date=in.next();//reading data from console
String dmy[]=date.split("/");
if(dmy.length!=3||dmy[0].length()!=2||dmy[1].length()!=2||dmy[2].length()!=4)//validating date according to mm/dd/yyyy format
{
main(args);
return;
}
int leap=0;//variable to check leap year
int days=0;//variable to store number of days
int month=0;//variable to store number of months
String error="";//variable used store error string
try
{
for(int i=dmy.length-1;i>=0;i--)
{
int temp=new Integer(dmy[i]);
if(i==2)
{
if((temp%4==0&&temp%100!=0)||temp%400==0)//check leap year
{
leap=1;
}
}
if(i==1)
{
if(temp>12)//error when month is>12
{
error="month value is greater than 12";
throw new NumberFormatException();
}
if(temp==4||temp==6||temp==9||temp==11)//computing days in a month
{
days=30;
month=temp;
}
else if(temp==2)//computing days in a month
{
if(leap==1)
{
days=29;
}
else
{
days=28;
}
month=temp;
}
else//computing days in a month
{
days=31;
month=temp;
}
}
if(i==0)
{
if(temp>days)//formating error string
{
error="number of days in month "+month+" is"+days+" but input contains "+temp+" days";
throw new NumberFormatException();
}
}
}
}
catch(NumberFormatException e)
{
System.out.println(error);
main(args);
return;
}
System.out.println("Input date is "+date+" date is valid");//printing if it is valid month
}
}
Explanation / Answer
import java.util.Scanner; public class Q1 { public static void main(String[] args) { System.out.print("enter date containing format mm/dd/yyyy:"); final Scanner in = new Scanner(System.in); String date=in.nextLine();//reading data from console String dmy[]=date.split("/"); if(dmy.length!=3||dmy[0].length()!=2||dmy[1].length()!=2||dmy[2].length()!=4)//validating date according to mm/dd/yyyy format { main(args); return; } int leap=0;//variable to check leap year int days=0;//variable to store number of days int month=0;//variable to store number of months String error="";//variable used store error string try { int temp=new Integer(dmy[2]); if((temp%4==0&&temp%100!=0)||temp%400==0)//check leap year { leap=1; } temp=new Integer(dmy[0]); if(temp>12)//error when month is>12 { error="month value is greater than 12"; throw new NumberFormatException(); } temp=new Integer(dmy[0]); if(temp==4||temp==6||temp==9||temp==11)//computing days in a month { days=30; month=temp; } else if(temp==2)//computing days in a month { if(leap==1) { days=29; } else { days=28; } month=temp; } else//computing days in a month { days=31; month=temp; } temp=new Integer(dmy[1]); if(temp!=days)//formating error string { error="number of days in month "+month+" is"+days+" but input contains "+temp+" days"; throw new NumberFormatException(); } } catch(NumberFormatException e) { System.out.println(error); main(args); return; } System.out.println("Input date is "+date+" date is valid");//printing if it is valid month } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.