Results 1 to 6 of 6
- 02-07-2011, 07:37 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
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 09:28 PM.
- 02-07-2011, 07:42 PM #2
Member
- Join Date
- Feb 2011
- Posts
- 30
- Rep Power
- 0
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.
- 02-07-2011, 08:00 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
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....
- 02-07-2011, 08:17 PM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
For month problem you can use a String[] with names of months...
- 02-07-2011, 09:38 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
Any suggestions on how to fix the day of the week?
- 02-08-2011, 10:53 PM #6
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
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
-
Second week into Java - errors
By hayden06f4i in forum New To JavaReplies: 9Last Post: 11-03-2010, 05:01 PM -
Day of the Week Calculator
By scheng12 in forum New To JavaReplies: 0Last Post: 09-08-2009, 03:59 PM -
Get dates of current week
By Qlubbie in forum New To JavaReplies: 2Last Post: 11-19-2008, 09:03 AM -
Trouble getting Julia fraction correct.
By yllawwally in forum New To JavaReplies: 0Last Post: 12-21-2007, 02:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks