Hi guys, it's my first semester of computer science and I'm having a bit of trouble with the second assignment.
Basically, it's supposed to be a program that asks users to input a date (year month day). First, it's supposed to print a proper date (January 1, 2005), then it's supposed to give out the day number (January 1, 2005 = Day 1 of the year) and finally it's supposed to print the NEXT date (January 2, 2005).
I don't want the solution to this, just a nudge in the right direction. Thanks.
EDIT: Is it better if I associate the month days with constants? (final int JANUARY = 31)?
Code:
//********************************************************************************
//AUTHOR: RM
//Purpose: 1. Convert year/month/day into proper date (April 30, 2005)
//. 2. Display the day number. (April 30th, 2005 = Day 120 in the year)
// 3. Display the NEXT date of inputted date (May 1, 2005)
//Last modified: September 17th, 2005
//********************************************************************************
import java.util.*;
public class Date
{
//Initate scanner
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
//Variable declaration
int year;
int month;
int dayOfMonth;
int numOfDays;
int dayNumber
String monthName = "";
//Prompt user for input
System.out.print("Please enter a date (year month day): ");
year = keyboard.nextInt();
month = keyboard.nextInt();
dayOfMonth = keyboard.nextInt();
//Exclude invalid entries (will give more information to user than just using Switch default cases)
if (dayOfMonth > 31 || dayOfMonth < 1 || month > 12 || month < 1 || year < 1583)
{
System.out.println("\nPlease enter a valid date. Invalid date entries include:\n");
System.out.println("1. Days larger than 31 OR Days lower than 1");
System.out.println("2. Months larger than 12 OR Months lower than 1");
System.out.println("3. Years below 1583\n");
System.exit(0);
}
if (dayOfMonth > 30 && (month == 4 || month == 6 || month == 9 || month == 12))
{
System.out.println("\nThe months of April, June, September and November cannot have more than 30 days.");
System.out.println("Please re-run the program\n");
System.exit(0);
}
if (dayOfMonth > 29 && month == 2)
{
System.out.println("\nThe month of February cannot have more than 29 days.");
System.out.println("Please re-run the program\n");
System.exit(0);
}
//FIRST switch statement (Store number of days of months in variable)
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numOfDays = 31; break;
case 4:
case 6:
case 9:
case 11:
numOfDays = 30; break;
case 2:
if ( (year % 400 == 0) || ((year % 4 == 0) && !(year % 100 == 0)) )
numOfDays = 29;
else
numOfDays = 28; break;
default: break;
}
//SECOND switch statement (store month name in String variable)
switch (month)
{
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "August"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December"; break;
default: break;
}
//Calculate day number
}
}