# HELP! Trying to find the days between two dates WITHOUT gregorian calendar.

• March 13th, 2011, 05:54 PM
CodyS
HELP! Trying to find the days between two dates WITHOUT gregorian calendar.
hey so this is my first post and I really need help. my friend and I cannot figure out why we cannot get all of the answers. This is the assignment:
Quote:

Write a program to accept any two dates in the form of month, day, and year (8 23 2000), separated by spaces, and calculate the total number of days that has elapsed between the two dates, inclusive of the beginning and ending days. Remember that leap year is a year divisible by 4, except for centennial years, which must be divisible by 400, such as 1600 or 2000 (1800 is not a leap year). You can test your program with any typed in dates but it must finally run with the data shown below. System output on the screen is acceptable.

7 4 1776 1 1 1987 (76882)
2 1 1983 3 3 1984 (397)
4 7 1983 11 4 1983 (???)
7 3 1983 7 20 1983 (18)
12 29 1990 12 31 1992 (???)
3 17 1992 3 17 2011 (????)

The program must continue running until all the data is processed – use a loop 1 to 6 or a while (month != -1) or while (!fin.eof()) - choose your own end of data technique.
Use if statements, switch statements, and loops to solve this problem. Do not use the Gregorian date method!
The data can be read from a file using the Scanner class attached to a text file, as in:
Scanner scan = new Scanner (new File (“input.txt”));
and here is my completed code:

Code Java:

```import java.util.Scanner; import java.io.*;   public class diffsInDates { public static void main(String[] args) throws IOException { int m1= 0 , d1= 0 , y1= 0 , m2= 0 , d2= 0, y2= 0 ; Scanner scan = new Scanner( new FileReader("dates.txt"));   for(int i = 0 ; i < 6 ; i++)// to keep loop active until all data is scanned. {   m1 = scan.nextInt(); // integers that are scanned in. d1 = scan.nextInt(); y1 = scan.nextInt(); m2 = scan.nextInt(); d2 = scan.nextInt(); y2 = scan.nextInt();   System.out.println("Total number of days between dates entered are: " + x(m1, m2, d1, d2, y1, y2) ); } } public static int x (int m1, int m2, int d1, int d2, int y1, int y2) { int result = 0; int result1 = 0; int result2 = 0;   if( m1 == m2 && y1 == y2) { // counts days if year and month are the same. result = d2 - d1 +1; }   else if ( y1 == y2 ) { result = daysInMonth( m2 , y2 ) - d1 + 1; }   for(int i = m1; i < m2 ; i++) // counts months { result1 += daysInMonth( m2 , y2 ); }   for (int i = y1; i < y2; i++) { // counts years result2 += daysInYear( y2 ); }   result += result1 + result2; return result; }   //methods   public static boolean isLeap(int year) // to find if it is a leap year { if( year % 400 == 0) return true;   else if( year % 4 == 0 && year % 100 != 0) return true;   else return false; }     public static int daysInMonth( int month , int year) // to find days in the month { int leapMonth;   if (isLeap(year)== true) { leapMonth = 29; } else { leapMonth = 28; }   switch(month) { case 4: case 6: case 9: case 11: return 30; case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 2: return leapMonth; } return 28; }   public static int daysInYear(int year) // to find the days in the year, leap or not. { if( isLeap(year)){ return 366; } else{ return 365; } } }```
Total number of days between dates entered are: 77015 (wrong)
Total number of days between dates entered are: 397
Total number of days between dates entered are: 234 (wrong)
Total number of days between dates entered are: 18
Total number of days between dates entered are: 732 (wrong)
Total number of days between dates entered are: 6935 (wrong)

THANK YOU for any help.
• March 13th, 2011, 06:52 PM
CodyS
Re: HELP! Trying to find the days between two dates WITHOUT gregorian calendar.
its due on 3/17/2011.
• March 13th, 2011, 08:57 PM
vanDarg
Re: HELP! Trying to find the days between two dates WITHOUT gregorian calendar.
Make sure you use break statements in your switch.
• March 14th, 2011, 11:34 AM
CodyS
Re: HELP! Trying to find the days between two dates WITHOUT gregorian calendar.
I tried that and it didnt work. it said it was unreachable :/
• March 14th, 2011, 12:00 PM
JavaPF
Re: HELP! Trying to find the days between two dates WITHOUT gregorian calendar.
You are not using the switch statement correctly. I don't think you can use it like that!

Oracle Tutorials - Switch Statement
• March 14th, 2011, 01:27 PM
aussiemcgr
Re: HELP! Trying to find the days between two dates WITHOUT gregorian calendar.
I suspect your switch statement is being interpreted like this:
Code java:

```case 4: { case 6: { case 9: { case 11: { return 30; } } } } case 1: { case 3: { case 5: { case 7: { case 8: { case 10: { case 12: { return 31; } } } } } } } case 2: { return leapMonth; }```

If I am correct, removing the cases that do not have return statements should theoretically work. Give it a try.
• March 14th, 2011, 02:31 PM
copeg
Re: HELP! Trying to find the days between two dates WITHOUT gregorian calendar.
The switch statement is perfectly legit. Nothing below a case means it will fall through to the next until a condition is reached. Further, returning a value inherently breaks the switch (so no breaks needed).

Alternatively, inspect the logic in how the value is calculated in the 'x' function (BTW you might want to consider naming your variables and methods with a bit more meaning - helps us and you). For example:

1) what if m1 > m2 (hint - the loop using these values won't execute)?
2) What are you passing to daysInMonth function (hint - its the same month value for every iteration - don't you want to iterate over them)?
3) An alternative approach you might consider is to first find the earliest date - then count up from there.

These most likely are not the only issues but should point you in the right direction. More importantly, add some println statements in there to debug, and go through the iterative process of modifying code and inspecting its behavior using printlns (see my recent blog post for a simple tutorial)
• March 14th, 2011, 05:55 PM
vanDarg
Re: HELP! Trying to find the days between two dates WITHOUT gregorian calendar.
Another piece of advice on debugging, if you are using Eclipse IDE, it has a really nice "debug" function. It allows you to see the program run, line by line.
• March 16th, 2011, 11:21 AM
CodyS
Re: HELP! Trying to find the days between two dates WITHOUT gregorian calendar.
So I was able to figure it out. Thanks for all of your help. I may post the code later.
• March 16th, 2011, 11:35 AM
CodyS
Re: HELP! Trying to find the days between two dates WITHOUT gregorian calendar.
Code :

```import java.util.Scanner; import java.io.*;   public class diffsInDates { public static void main(String[] args) throws IOException { int m1 , d1 , y1 , m2 , d2, y2 ;   Scanner scan = new Scanner( new FileReader("dates.txt"));   for(int i = 0 ; i < 6 ; i++) { m1 = scan.nextInt(); d1 = scan.nextInt(); y1 = scan.nextInt(); m2 = scan.nextInt(); d2 = scan.nextInt(); y2 = scan.nextInt();   System.out.println("Total number of days between dates entered are: " + x( m1, m2, d1, d2, y1, y2) ); } } public static int x (int m1, int m2, int d1, int d2, int y1, int y2) { int result = 0;   if( m1 == m2 && y1 == y2) result = d2 - d1 +1;   else if ( y1 == y2 ) { result = daysInMonth( m1 , y1 ) - d1 + 1;   for(int i = m1 +1; i < m2 ; ++i) { result += daysInMonth( i , y1 ); } result += d2; }   else { result = daysInMonth( m1 , y1 ) - d1 + 1;   for(int i = m1 + 1; i <= 12 ; ++i) { result += daysInMonth( i , y1 ); }   for (int i = y1 + 1 ; i < y2; i++) { result += daysInYear( i ); }   for(int i = 1 ; i < m2 ; ++i) { result += daysInMonth( i , y2); }   result += d2; } return result; }   public static boolean isLeap(int year) //METHODS { if( year % 400 == 0) return true;   else if( year % 4 == 0 && year % 100 != 0) return true;   else return false; }     public static int daysInMonth( int month , int year) { switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: if (isLeap(year)) return 29; else return 28; default: return 31; } }   public static int daysInYear(int year) { if( isLeap(year)) return 366; else return 365; } }```