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:
and here is my completed code: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.
DATA: ANSWERS
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”));
the answers I get are: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.