-
Need help with program
I'm getting a Java 157: ')' expected
return year % 400 == 0 || (year % 4 == 0 &&&& year % 100 137 != 0);
^
Can someone please help me with this??
import javax.swing.JOptionPane;
public class PrintCalendar {
/** Main method */
public static void main(String[] args) {
// Prompt the user to enter year
String yearString = JOptionPane.showInputDialog(
"Enter full year (e.g., 2001):");
// Convert string into integer
int year = Integer.parseInt(yearString);
//Prompt the user to enter month
String monthString = JOptionPane.showInputDialog(
"Enter month in number between 1 and 12:");
//Convert string into integer
int month = Integer.parseInt(monthString);
//Print calendar for the month of the year
printMonth(year, month);
}
/** Print the calendar for a month in a year */
static void printMonth(int year, int month) {
//Print the heading of the calendar
printMonthTitle(year, month);
//Print the headings of the calendar
printMonthBody(year, month);
}
/** Print the month title, e.g., May, 1999 */
static void printMonthTitle(int year, int month) {
System.out.println(" " + getMonthName(month)
+ " " + year);
System.out.println("------------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
}
/** Get the English name for the month */
static String getMonthName(int month) {
String monthName = null;
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;
}
return monthName;
}
/** Print month body*/
static void printMonthBody(int year, int month){
//Get start day of the week for the first date in the month
int startDay = getStartDay(year, month);
//Get number of days in the month
int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
//Pad space before the first day of the month
int i = 0;
for (i = 0; i < startDay; i++)
System.out.print(" ");
for (i = 1; i <= numberOfDaysInMonth; i++) {
if (i < 10)
System.out.print(" " + i);
else
System.out.print(" " + i);
if ((i + startDay) % 7 == 0)
System.out.println();
}
System.out.println();
}
/** Get the start day of the first day in a month */
static int getStartDay(int year, int month) {
//Get total number of days since 1/1/1800
int startDay1800 = 3;
int totalNumberOfDays = getTotalNumberOfDays(year, month);
//Return the start day
return (totalNumberOfDays + startDay1800) % 7;
}
/** Get the total number of days since Jan 1, 1800 */
static int getTotalNumberOfDays(int year, int month) {
int total = 0;
// Get the total days from 1800 to year - 1
for (int i = 1800; i < year; i++)
if (isLeapYear(i))
total = total + 366;
else
total = total + 365;
// Add days from Jan to the month prior to the calendar month
for (int i = 1; i < month; i++)
total = total + getNumberOfDaysInMonth(year, i);
return total;
}
/** Get the number of days in a month */
static int getNumberOfDaysInMonth(int year, int month) {
if (month == 1 || month == 3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12)
return 31;
if (month == 4 || month == 6 || month == 9 || month == 11)
return 30;
if (month == 2) return isLeapYear(year) ? 29 : 28;
return 0; // If month is incorrect
}
/** Determine if it is a leap year */
static boolean isLeapYear(int year) {
return year % 400 == 0 || (year % 4 == 0 && year % 100 137 != 0);
}
}
-
Try this:
Code:
return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
Happiness is good health and a bad memory.
-
Just a remark: You should perhaps go for zero-based months, as that is quite
common in date/calendar classes.
And: these types of classes gets easier to handle (read) if you use a static
matrix for months, like:
Code:
public final static Object [][] MTHS={
{new Integer(31),"Jan","January"},
{new Integer(28),"Feb","February"},
{new Integer(31),"Mar","March"},
{new Integer(30),"Apr","April"},
{new Integer(31),"May","May"},
{new Integer(30),"Jun","June"},
{new Integer(31),"Jul","July"},
{new Integer(31),"Aug","August"},
{new Integer(30),"Sep","September"},
{new Integer(31),"Oct","October"},
{new Integer(30),"Nov","November"},
{new Integer(31),"Dec","December"}
};
public static String getMonthName(int mth) {
return (String)MTHS[mth][2];
}
public static String getMonthShortName(int mth) {
return (String)MTHS[mth][1];
}
public static int getMonthDays(int mth) {
return ((Integer)MTHS[mth][0]).intValue();
}
public static int getMonthDays(int mth, int year) {
int days = ((Integer)MTHS[mth][0]).intValue();
if (mth != 1) return days;
return (PrintCalendar.isLeapYear(year)) ? 29 : 28;
}
This code is in peril of throwing an IndexOutOfBoundsException.
If it should be trapped in the actual method or be handled by the caller
depends on the implementation.
Last edited by sjalle; 11-02-2005 at 07:05 AM.
eschew obfuscation
Similar Threads
-
By mheasen in forum Architecture and Design
Replies: 0
Last Post: 03-20-2002, 09:24 AM
-
Replies: 2
Last Post: 03-07-2002, 07:49 AM
-
By Gordon Reichhardt in forum VB Classic
Replies: 2
Last Post: 01-08-2002, 10:06 AM
-
By W.Pierce in forum VB Classic
Replies: 1
Last Post: 12-11-2001, 08:28 AM
-
Replies: 0
Last Post: 12-15-2000, 10:07 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks