Results 1 to 6 of 6
  1. #1
    dexter is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default Having trouble getting the correct day of the week???

    Hello everyone.... Been working on a code that has me very burned out any help would be greatly appreciated...

    The user enters the day, month and year... so the output should give you day, month and year plus what day of the week is it (Sunday, Monday, Tuesday, etc...)...
    I will also like on the output to spell out the month instead of giving you the interger value... I still havent written anything on how to do that been busy on the rest and not sure where to start on doing that...

    This is what i have so far... Thx :)


    import java.io.*;
    import java.util.*;


    public class Example3 {

    public static void main(String[] args) throws IOException {
    int mm, dd, yyyy, zr, yy, c, day, i;
    String days[] = {"Tuesday", "Wednesday", "Thursday", "Friday",
    "Saturday", "Sunday", "Monday"};
    int maxDays [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter the month (1-12): ");
    mm = keyboard.nextInt();

    System.out.print("Enter the day: ");
    dd = keyboard.nextInt();

    System.out.print("Enter the year: ");
    yyyy = keyboard.nextInt();

    //advance date by one day

    //test if year is leap year
    if (yyyy % 4 == 0) {
    if (yyyy % 100 != 0){
    maxDays[1] = 29;
    }
    else if(yyyy % 400 == 0){
    maxDays[1] = 29;
    }
    }

    if (dd > maxDays[mm-1]) {
    dd = 1;
    mm = mm + 1;
    }

    if (mm > 12) {
    mm = 1;
    yyyy = yyyy + 1;
    }

    //calculate day of the week, Zeller's Rule
    yy = yyyy % 100; // get last 2 digits of year
    c = yyyy / 100; // get first 2 digits of year, century
    zr = dd + ((13*mm-1)/5) + yy + (yy/4) + (c/4) - 2*c;
    day = zr % 7;
    if (day < 0){ // if day is negative
    i = Math.abs(day) % 7;
    i++;
    day = ((7 * i ) + day) % 7;
    }

    System.out.println("Date is: " + mm + "/" + dd + "/" +
    yyyy + " is a " + days[day]);
    }
    }

    Output***
    Enter the month (1-12): 1
    Enter the day: 4
    Enter the year: 2011
    Date is: 1/4/2011 is a Sunday


    The error is that the date does not fall on a Sunday that paticular date dalls on a Tuesday... For some reason when i type a certain date it will give me the correct results but other dates are incorrect... I also want to add names for each month that way when i run the program it could say something like.. January 12, 2011 is a Wednesday....
    Last edited by dexter; 02-07-2011 at 10:28 PM.

  2. #2
    codesmuggler is offline Member
    Join Date
    Feb 2011
    Posts
    30
    Rep Power
    0

    Default

    1. Use sysouts to determine where the bug is.
    2. Show some input and output of your code - and what you expect.
    3. Use [code] tags.

  3. #3
    dexter is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Output***
    Enter the month (1-12): 1
    Enter the day: 4
    Enter the year: 2011
    Date is: 1/4/2011 is a Sunday


    The error is that the date does not fall on a Sunday that paticular date dalls on a Tuesday... For some reason when i type a certain date it will give me the correct results but other dates are incorrect... I also want to add names for each month that way when i run the program it could say something like.. January 12, 2011 is a Wednesday....

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    For month problem you can use a String[] with names of months...

  5. #5
    dexter is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Any suggestions on how to fix the day of the week?

  6. #6
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    See if this is working and read my comments in the code


    Java Code:
    import java.io.*;
    import java.util.*;
    
    
    public class Example3 {
    
        public static void main(String[] args) throws IOException {
    		
            new Example3();
        }	
        public Example3(){
    		
            int mm, dd, yyyy, zr, yy, c, day, i, zmm, zyyyy; // added new variables just to be more readable
    		
            // zeller's rule says reminder 0 (your zr) corresponds to Sunday, 1 corresponds to Monday
            // so I rearanged days in that order
            String days[] ={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday","Saturday" };
    		
            // I dont see need for this array
            //int maxDays [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Enter the month (1-12): ");
            mm = keyboard.nextInt();
    
            System.out.print("Enter the day: ");
            dd = keyboard.nextInt();
    
            System.out.print("Enter the year: ");
            yyyy = keyboard.nextInt();
    
            // Zeller's rule already calculates leap year so you don't need this part of code...
    		
            //advance date by one day
    
            //test if year is leap year
            //if (yyyy % 4 == 0) {
            //    if (yyyy % 100 != 0){
            //        maxDays[1] = 29;
            //    }
            //	else if(yyyy % 400 == 0){
            //       maxDays[1] = 29;
            //	}
            //}
            //if (dd > maxDays[mm-1]) {
            //	dd = 1;
            //	mm = mm + 1;
            //}
            //if (mm > 12) {
            //	mm = 1;
            //	yyyy = yyyy + 1;
            //}
    		
            zyyyy = yyyy;
    		
            // Zeller's rule says that we start year from March=1, April=2...
            // this part of code handles that
    		
            // so Zeller's month is:
            zmm = mm - 2;
            if(zmm <= 0){
                zmm = zmm + 12;
                // in this case Zeller's year is
                zyyyy--;
            }
    		
            //calculate day of the week, Zeller's Rule
            yy = zyyyy % 100; // get last 2 digits of year
            c = zyyyy / 100; // get first 2 digits of year, century
            zr = dd + ((13*zmm-1)/5) + yy + (yy/4) + (c/4) - 2*c;
            day = zr % 7;
            if (day < 0){ // if day is negative
    	i = Math.abs(day) % 7;
    	i++;
    	day = ((7 * i ) + day) % 7;
            }
    
            System.out.println("Date is: " + mm + "/" + dd + "/" + yyyy + " is a " + days[day]);
            }
        }

Similar Threads

  1. Second week into Java - errors
    By hayden06f4i in forum New To Java
    Replies: 9
    Last Post: 11-03-2010, 06:01 PM
  2. Day of the Week Calculator
    By scheng12 in forum New To Java
    Replies: 0
    Last Post: 09-08-2009, 04:59 PM
  3. Get dates of current week
    By Qlubbie in forum New To Java
    Replies: 2
    Last Post: 11-19-2008, 10:03 AM
  4. Trouble getting Julia fraction correct.
    By yllawwally in forum New To Java
    Replies: 0
    Last Post: 12-21-2007, 03:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •