Question
Java Project Name: IC06_ValidDate Write a Java console application that reads a string from the keyboard and tests whether it contains a valid date. Display
Java Project Name: IC06_ValidDate
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)***
i got my code I just don't know why it isn working can anybody help me
import java.util.Scanner;
public class ValidDate {
public static void main(String[] args) {
Scanner consoleScanner = new Scanner(System.in);
boolean validDate;
// set the date true
validDate = true;
boolean invalidDate;
// set the date to false;
invalidDate = false;
String InputDate = null;
String monthPart = InputDate.substring(0, 2);
int month = Integer.parseInt(monthPart);
String dayPart = InputDate.substring(3,5);
int day = Integer.parseInt(dayPart);
String yearPart = InputDate.substring(6,10);
int year = Integer.parseInt(yearPart);
// now do the input
System.out.println("Enter a Date (mm/dd/yyyy): ");
//Get input & use a delimiter. Delimiter means if you want a symbol to come out in the output and work
consoleScanner.useDelimiter("[/ ]");
month = consoleScanner.nextInt();
day = consoleScanner.nextInt();
year = consoleScanner.nextInt();
if((month >= 1 && month <= 12) && (day >= 1 && day <= 31))
{
//For months with 30 days
if((month == 4 || month == 6 || month == 9 || month == 11) && (day <= 30))
{
validDate = true;
}
//For months with 31 days
if((month == 1 || month == 2 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day <= 31))
{
validDate = true;
}
// for invalid date
else if((month < 1 && month > 12) && (day < 1 && day >31))
{
invalidDate = false;
}
//For months with 30 days
else if((month == 1 || month == 2 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month >=12) && (day <= 30))
{
invalidDate = false;
}
//For months with 31 days
else if((month == 4 || month == 6 || month == 9 || month == 11 || month >=12) && (day <= 31))
{
invalidDate = false;
}
//If the date is valid
if(validDate == true)
{
System.out.println(month + "/" + day + "/" + year + " is a valid date.");
}
else
{
System.out.println(invalidDate + "Invalid date!");
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started