Lab 3: Date Format

20.00 $

Categories: ,

Description

5/5 - (1 vote)

Lab 3: Date Format
Write a program that reads a string from the keyboard and tests whether it contains a valid date. Display the date and 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). the day value dd must be from 1 to a value that is appropriate for the given moth. September, April, June,
and November each have 30 days. February has 28 days except for the leap years when it has 29. The remaining months all have 31 days each. A leap year is any year that is divisible by 4 but not divisible by 100 unless it is
also divisible by 400.
2 Hints
To complete this assignment, you will need to utilize some methods not used in class. Start by reading in the user’s input using Scanner’s nextLine()
method.
2.1 Substring
Next we have to get the individual numbers out of the string. First to get
a small portion of the string, use the substring(start, end) method.
Here is an example:
String s = “My name”;
String s2 = s.substring(0,2); // holds the value “my”
String s3 = s.substring(3,6); // holds the value “nam”
String s4 = s.substring(3); // holds the value “name”
String space = s.substring(2,3); // holds the value ” ”

substring() returns a new string having the same characters as the portion of the original that starts a index start and up to, but not including end. Indices in Java start at 0, so the rst character is at index 0, the second
at index 1, and so on. If no end is speci ed, the substring goes until the end of the String
2.2 Converting Strings to Integers
Integer.parseInt(someString) converts a string into an int.
String s = “123”;
int i = Integer.parseInt(s); //stores 123
Use the above information to get integer values for the month, date, and
year. The rest of the program is logic!

  • Lab3.zip