Problem: The purpose of this assignment is to gain experience solving user input
ID: 3598782 • Letter: P
Question
Problem:
The purpose of this assignment is to gain experience solving user input problems by using exception handling in your Java code.
Write a program that reads a string from the keyboard and tests whether it contains a valid date. Display the date as described below if it is valid, otherwise display a message as described below. The input date should have the format mm/dd/yyyy, where mm and dd can be one or two digits.
The program solution should follow good programming development principles and be coded as a number of methods.
Here are the input errors (exceptions) that your program should detect and deal with:
Receive the input from the user (give an example of the format you are expecting the user to use when entering the date) and check for the following:
missing one of parts (that is the month or the day or the year is missing. Check this by checking the number of slashes (/)), eg mm/dd is invalid
mm or dd or yyyy is not numeric
month not between 1 and 12
day is not in the right range (this depends on the month)
year isn’t 2000 or higher
Be specific when reporting an error. For example, “The input 13/4/2000 contains an invalid month of 13” or “February doesn’t have 30 days”, etc.
If the user gives you invalid input, start the whole process again by asking the user for a valid date. Keep prompting the user until you get a valid date. A while (!valid) loop would work well here.
If the date is valid, then output a message in this format, “December 25, 2004 is a valid date”.
One of the checks should be for leap years. February has 28 days except for leap years when it has 29. A leap year (for the 2000s) is any year divisible by 4.
Notes:
String class methods (indexOf method) can be used to find the slashes (/). Strings can be converted to integers by using the Integer.parseInt( ) method.
Be sure that the output that you submit shows that your program has tested ALL the different types of errors that can occur.
Check for 5 types of exceptions:
SlashException – checks to see if the input string is missing one of the slashes (/), which separates the month and day, and the day and year
NumberFormatException – if the input string has the correct format, ie mm/dd/yyyy, then determine if the input consists of numbers only (ie, no non-numeric characters other than /). The easiest way to do this is to use the parseInt method from the Integer class, ie Integer.parseInt(mm) or parseInt(dd). parseInt( ) will throw a NumberFormatException if mm or dd can’t be converted to an integer
MonthException: month not between 1 and 12
YearException: year is not between 2000 and 2017
MonthDayException: month-day combinations is not ok, for example check that February doesn’t have 30 days, etc.
You may structure your solution however you wish but at least one method must throw at least 2 exceptions
The messages output by the catch blocks should be very specific. For example, output “123 is an invalid month”, or “abc is an invalid month”, rather than simply “invalid month”. The first 2 messages allow the user to better understand what input caused the problem; the last message leaves the user guessing about what the source of the problem is.
You do not need to write code for the NumberFormatException class; this already exists as part of java.
You’ll need to write class definition files for the other exceptions described above.
Explanation / Answer
import java.util.Scanner;
public class Ch3ProblemNine
{
public static void main(String [ ] args)
{
System.out.println("Enter the date in mm/dd/yyyy format. ");
String date="";
Scanner keyboard = new Scanner(System.in);
int mm = 00;
int dd = 00;
int yy = 0000;
date = keyboard.nextLine();
boolean isLeapYear;
mm=0;
dd=0;
yy=0;
isLeapYear=false;
if(yy%4==0 && (!(yy%100==0) || yy%400==0))
{
isLeapYear=true;
}
if((mm<12) && (mm>1))
{
System.out.println("You have entered an invalid month. Please try again.");
}
if((dd>31 && dd<1))
{
System.out.println("You have entered an invalid day. Please try again.");
}
if((mm==9 && mm==4 && mm==6 && mm==11) && !(dd==31))
{
System.out.println("For the given month, you have entered an invalid day.");
}
if((mm==2 && !(dd<29)) && isLeapYear==false)
{
System.out.println("You have entered an invalid day for the month of February.");
}
if((mm==2 && !(dd<30)) && isLeapYear==true)
{
System.out.println("You have entered an invalid day for the month of February.");
}
else
{
System.out.println("You have entered a valid date in the correct format.");
}
if (isLeapYear){
if((mm==2 && !(dd==29)) && isLeapYear==true)
System.out.println(date + " is a valid date.");
}
else
System.out.println( date + "is not valid month must have 29 days or less.");
if ((mm ==2) && (dd<=28))
System.out.println( date + " is a valid date.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.