/* Program Name: BadDate.java
Function: This program determines if a date entered by the user is valid.
Input: Interactive
Output: Valid date is printed or user is alerted that an invalid date was entered.
*/
import java.util.Scanner;
public class BadDate
{
public static void main(String args[])
{
// Declare variables
Scanner userInput = new Scanner (System.in);
String yearString;
String monthString;
String dayString;
int year;
int month;
int day;
boolean validDate = true;
final int MIN_YEAR = 0, MIN_MONTH = 1, MAX_MONTH = 12, MIN_DAY = 1, MAX_DAY = 31;
// This is the work of the housekeeping() method
// Get the year, then the month, then the day
Scanner input = new Scanner(System.in);
// variable = input.nextLine();
System.out.println("Enter the Month");
monthString = input.nextLine();
System.out.println("Enter the Day");
dayString = input.nextLine();
System.out.println("Enter the Year");
yearString = input.nextLine();
// Convert Strings to integers
month = Integer.parseInt(monthString);
day = Integer.parseInt(dayString);
year = Integer.parseInt(yearString);
// This is the work of the detailLoop() method
// Check to be sure date is valid
if( year <= MIN_YEAR ) // invalid year
validDate = false;
if ( month < MIN_MONTH || month > MAX_MONTH ) // invalid month
validDate = false;
if ( day < MIN_DAY || day > MAX_DAY ) // invalid day
validDate = false;
// This is the work of the endOfJob() method
// Test to see if date is valid and output date and whether it is valid or not
if( validDate == true )
{ System.out.println("(month)/ (day)/ (year) is a valid date");
// Output statement
}
else
{
System.out.println("(month)/ (day)/ (year) is an invalid date");
// Output statement
}
} // end of main() method
} // end of BadDate class
HOW DO I GET AN INVALID AND VALID INPUT?
Note: I have highlighted the modified code . Do Check it.
Code to copy:
/* Program Name: BadDate.java
Function: This program determines if a date entered by
the user is valid.
Input: Interactive
Output: Valid date is printed or user is alerted that
an invalid date was entered.
*/
import java.util.Scanner;
import javax.swing.JOptionPane;
public class BadDate
{
public static void main(String
args[])
{
// Declare
variables
Scanner
userInput = new Scanner (System.in);
String
yearString;
String
monthString;
String
dayString;
int year;
int month;
int day;
boolean
validDate = true;
final int
MIN_YEAR = 0, MIN_MONTH = 1, MAX_MONTH = 12, MIN_DAY = 1, MAX_DAY =
31;
// This is the
work of the housekeeping() method
// Get the year,
then the month, then the day
Scanner input =
new Scanner(System.in);
// variable =
input.nextLine();
System.out.println("Enter the Month");
monthString =
input.nextLine();
System.out.println("Enter the Day");
dayString =
input.nextLine();
System.out.println("Enter the Year");
yearString =
input.nextLine();
// Convert
Strings to integers
month =
Integer.parseInt(monthString);
day =
Integer.parseInt(dayString);
year =
Integer.parseInt(yearString);
// This is the
work of the detailLoop() method
// Check to be
sure date is valid
if( year <=
MIN_YEAR ) // invalid year
validDate = false;
if ( month <
MIN_MONTH || month > MAX_MONTH ) // invalid month
validDate = false;
if ( day <
MIN_DAY || day > MAX_DAY ) // invalid day
validDate = false;
// This is the
work of the endOfJob() method
// Test to see
if date is valid and output date and whether it is valid or
not
if( validDate ==
true )
{
System.out.println(month+"/"+day+"/"+year
+ " is a valid date");
// Output statement
}
else
{
System.out.println(month+"/"+day+"/"+year
+ " is an invalid date");
// Output statement
}
}// end of main() method
} // end of BadDate class
Output Screenshot:
Get Answers For Free
Most questions answered within 1 hours.