Write a Java application program that will allow the user to enter a date. The p
ID: 3872663 • Letter: W
Question
Write a Java application program that will allow the user to enter a date. The program will then check the date to verify whether or not it is a valid date Begin by asking the user to enter a date in the form, mm/dd/yyyy. This will be entered as a String, since the date contains slash marks. After the date is entered, the program should check the date for validity. If the date is valid, the program should display the message "Valid date". If the date is not valid, the program should display the message "Not a valid date" along with a message to indicate why the date is not valid To check the date: . Begin by checking to make sure the user has entered enough characters. A correct date should consist of exactly 10 characters - no more and no less. If the user has not entered the correct number of characters, display whichever of the following messages is appropriate: Too few characters in the date Too many characters in the date . If the first test turned out to be ok, next you should check to make sure that the slash marks are in the correct positions. In a correctly entered date, a slash mark should be the third character in the string, and also the sixth character in the string. If you do not find slash marks at these two points, display the following message: Incorrect format .If the first two tests turned out to be ok, next you should check the month. You will need to extract the month part from the string. Since it will be extracted as a string, you will then need to convert it to an integer so you can check it's value. A valid month value must be from 1 to 12. If the month is not in the proper range, display the following message: Month is not valid .If the first three tests turned out to be ok, next check the day. You will need to extract the day part from the string. Since it will be extracted as a string, you will then need to convert it to an integer so you can check it's value. The day value should not be less than 1. It should also not be greater than 28, 29, 30 or 31; whichever is appropriate for the given month. September, April lUrExplanation / Answer
MODIFIED------ MODIFIED
_________________
ValidDateOrNot.java
import java.util.Scanner;
public class ValidDateOrNot {
public static void main(String[] args) {
// Declaring an array and initializing corresponding days in the month
int months[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// Declaring variables
String str;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
/*
* This while loop continues to execute until the user enters a valid
* date
*/
while (true) {
System.out.print("Enter a date :");
str = sc.next();
if (str.length() < 10) {
System.out.println("Not a valid date");
System.out.println("Too few characters in the date");
continue;
} else if (str.length() > 10) {
System.out.println("Not a valid date");
System.out.println("Too many characters in the date");
continue;
} else if (str.charAt(2) != '/' || str.charAt(5) != '/') {
System.out.println("Not a valid date");
System.out.println("Incorrect Format");
continue;
} else if (Integer.parseInt(str.substring(0, 2)) < 0 || Integer.parseInt(str.substring(0, 2)) > 12) {
System.out.println("Not a valid date");
System.out.println("Month is not valid");
continue;
} else if (isleap(Integer.parseInt(str.substring(6, 10)))) {
if (Integer.parseInt(str.substring(0, 2)) == 2 && Integer.parseInt(str.substring(3, 5)) > 29) {
System.out.println("Not a valid date");
System.out.println("Day is not valid");
continue;
} else if (Integer.parseInt(str.substring(0, 2)) == 2 && (months[Integer.parseInt(str.substring(0, 2)) - 1] <= 29)) {
System.out.println("Valid date");
break;
} else if (Integer.parseInt(str.substring(3, 5)) <= months[Integer.parseInt(str.substring(0, 2)) - 1]) {
System.out.println("Valid date");
break;
}
} else if (Integer.parseInt(str.substring(0, 2)) == 2 && (months[Integer.parseInt(str.substring(0, 2)) - 1] > 28)) {
System.out.println("Not a valid date");
System.out.println("Day is not valid");
continue;
} else if (Integer.parseInt(str.substring(3, 5)) > months[Integer
.parseInt(str.substring(0, 2)) - 1]) {
System.out.println("Not a valid date");
System.out.println("Day is not valid");
continue;
} else {
System.out.println("Valid date");
break;
}
}
}
// This method will check whether the year is leap year or not
private static boolean isleap(int year) {
if ((year % 100 == 0 && year % 400 == 0) || (year % 100 != 0 && year % 4 == 0))
return true;
else
return false;
}
}
_____________________
Output:
Enter a date :123/456/7890
Not a valid date
Too many characters in the date
Enter a date :12/456/789
Not a valid date
Incorrect Format
Enter a date :13/15/1929
Not a valid date
Month is not valid
Enter a date :06/31/2009
Not a valid date
Day is not valid
Enter a date :02/29/2009
Not a valid date
Day is not valid
Enter a date :04/31/2017
Not a valid date
Day is not valid
Enter a date :02/29/2008
Valid date
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.