Need help with program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Need help with program

  1. #1
    Join Date
    Oct 2005
    Posts
    3

    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);
    }
    }

  2. #2
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    255
    Try this:

    Code:
    return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
    Happiness is good health and a bad memory.

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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

  1. problem in program in c++
    By mheasen in forum Architecture and Design
    Replies: 0
    Last Post: 03-20-2002, 09:24 AM
  2. Replies: 2
    Last Post: 03-07-2002, 07:49 AM
  3. Please help with running executable vb program.
    By Gordon Reichhardt in forum VB Classic
    Replies: 2
    Last Post: 01-08-2002, 10:06 AM
  4. How can I build copy protection into my program
    By W.Pierce in forum VB Classic
    Replies: 1
    Last Post: 12-11-2001, 08:28 AM
  5. 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center