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 thtat
Write a program that reads a string from the keyboard and tests whether it contains a valid date. Display the date and a message thtat indicates whether it is valid. if its 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 thtat is divisible by 4 but not divisible by 100 unless it is also divisible by 400
import java.util.Scanner; public class Ques1 { public static void main(String[] args) { int month, day, year; boolean validDate; System.out.println("Enter a date (mm/dd/yyyy)"); Scanner read = new Scanner(System.in); month = read.nextInt(); day = read.nextInt(); year = read.nextInt(); if((month >= 1 && month <= 12) && (day >= 1 && day <= 31)) { if((month == 4 || month == 6 || month == 9 || month == 11) && (day <= 30)) { validDate = true; } if((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day <= 31)) { validDate = true; } if((month == 2) && (day < 30)) { boolean validLeapYear = false; if((year % 400 == 0) || ((year % 4 == 0) && (year %100 !=0))) { validLeapYear = true; } if (validLeapYear == true && day <= 29) { validDate = true; } else if (validLeapYear == false && day <= 28) { validDate = true; } } } if(validDate == true) { System.out.println(month + "/" + day + "/" + year + " is a valid date."); } else { System.out.println("Invalid date!"); } } } i need help fixing this please thanks
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
