So, as some of you know, I’m studying Computer Science at Purdue University Calumet, in Indiana currently. I’m enrolled in a beginning Java course which has been relatively easy thus far, minus the occasional road bump of new concepts. Just wanted to see who all here is also interested in computers, programming, and the like.
Here’s an assignment I just finished, so you may feast your eyes upon the simplicity and beginner-level stuffs.
import java.util.Scanner;
public class Hw3
{
public static void main( String[] args)
{
System.out.println("Please enter a date in the mm/dd/yyyy format.");
Scanner keyboard = new Scanner (System.in);
keyboard.useDelimiter("/");
String userString = keyboard.nextLine();
int firstSlash = userString.indexOf("/");
int lastSlash = userString.lastIndexOf("/");
int length = userString.length();
String monthString = userString.substring (0, firstSlash);
if ( 2 != monthString.length())
{
System.out.println("Error with " + userString + ": The month must only be two digits.");
System.exit(0);
}
int month = Integer.parseInt ( monthString );
if (( month < 1 ) || ( month > 12))
{
System.out.println("Error with " + userString + ": The month must be between 1 and 12.");
System.exit(0);
}
String dayString = userString.substring ((firstSlash + 1), lastSlash);
if ( 2 != dayString.length())
{
System.out.println("Error with " + userString + ": The day must only be two digits.");
System.exit(0);
}
int day = Integer.parseInt ( dayString );
if ( day < 1)
{
System.out.println("Error with " + userString + ": The day cannot go lower than 1.");
System.exit(0);
}
String yearString = userString.substring((lastSlash + 1), length);
if ( 4 != yearString.length())
{
System.out.println("Error with " + userString + ": The year must only be four characters.");
System.exit(0);
}
int year = Integer.parseInt( yearString );
if (year < 1 )
{
System.out.println("Error with " + userString + ": Year cannot be less than 1.");
System.exit(0);
}
Boolean leapYear = true;
if ( 0 == (year % 4))
{
if ( 0 == (year % 100) && 0 == (year % 400))
{
leapYear = true;
}
else if ( 0 == (year % 100) && 0 != (year % 400))
{
leapYear = false;
}
}
else if ( 0 != (year % 4))
{
leapYear = false;
}
if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12))
{
if ( day > 31)
{
System.out.println("Error with " + userString + ": This month only has 31 days.");
System.exit(0);
}
}
else if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
{
if (day > 30)
{
System.out.println("Error with " + userString + ": This month only has 30 days.");
System.exit(0);
}
}
if (month == 2)
{
if (leapYear == true)
{
if (day > 29)
{
System.out.println("Error with " + userString + ": February only has 29 days this year.");
System.exit(0);
}
}
else if (leapYear == false)
{
if (day > 28)
{
System.out.println("Error with " + userString + ": February only has 28 days this year.");
System.exit(0);
}
}
}
System.out.println("The date " + userString + " is a correct date.");
System.exit(0);
}
}
This essentially receives a input through the keyboard in mm/dd/yyyy format for a date, runs it through a few checks, and then identifies whether it is a valid date, due to leap years or other logical aspects that make no sense for a date. Had issues with the leap year concept,but then decided on using a Boolean value for it and making it work. If it is invalid, it gives a reason why that’s so.