Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

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

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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”));
    and here is my completed code:

    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;
    			 }
    		}
    }
    the answers I get are:
    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.


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP! Trying to find the days between two dates WITHOUT gregorian calendar.

    its due on 3/17/2011.

  3. #3
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: HELP! Trying to find the days between two dates WITHOUT gregorian calendar.

    Make sure you use break statements in your switch.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 :/

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: HELP! Trying to find the days between two dates WITHOUT gregorian calendar.

    I suspect your switch statement is being interpreted like this:
    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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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)
    Last edited by copeg; March 14th, 2011 at 03:41 PM.

  8. #8
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  9. #9
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  10. #10
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP! Trying to find the days between two dates WITHOUT gregorian calendar.

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

Similar Threads

  1. 12 days of xmas??? not showing
    By chonch in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 09:45 PM
  2. Java/Excel integration, Reading in Dates
    By aussiemcgr in forum JDBC and Database Tutorials
    Replies: 0
    Last Post: July 16th, 2010, 08:38 AM
  3. UTC Dates
    By PedroCosta in forum Java Theory & Questions
    Replies: 4
    Last Post: April 1st, 2010, 11:39 AM
  4. how do i get only the workig days for a certain month
    By anonimus83 in forum Algorithms & Recursion
    Replies: 2
    Last Post: January 11th, 2010, 11:13 AM
  5. [SOLVED] Java program to sort arrays containing dates
    By scottyadam in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:08 PM