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 3 of 3

Thread: Problems with "if " statements in terms of leap years

  1. #1
    Junior Member
    Join Date
    Jan 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problems with "if " statements in terms of leap years

    Hi,
    I've been working in my free time on a project which tells what day someone is born based on their birthday. However, when I start to input birthdays greater than about a year old, the program is off by about a day. I believe this problem to be due to the leap year if statement I had, which I bolded in the code below. If you can offer any tips, suggestions, or hints, that would be amazing.

    Code:
    import java.util.Scanner;
    import static java.lang.System.in;
    import static java.lang.System.out;
    public class DayOfBirth {
    	public static void main (String args[]) {
    		Scanner scanned = new Scanner (in);
    		out.println("Which year were you born?");
    		int birthYear = scanned.nextInt();
    		out.println("What month were you born?(1 for January, 2 for February...)");
    		int birthMonth = scanned.nextInt();
    		out.println("What day were you born?");
    		int birthDate = scanned.nextInt();
     
    		int daysSince = 31 - birthDate;
    		int monthsSince = 12 - birthMonth;
    		int yearsSince = 2018 - birthYear;
    		int leapYearsSince = yearsSince / 4;
    		int monthDaysSince = 0;
    		switch (monthsSince) {
    		case 0:
    			monthDaysSince = 0;//From end of December
    			break;
    		case 1:
    			monthDaysSince = 31;//November
    			break;
    		case 2:
    			monthDaysSince = 61;//October
    			break;
    		case 3:
    			monthDaysSince = 92;//September
    			break;
    		case 4:
    			monthDaysSince = 122;//August
    			break;
    		case 5:
    			monthDaysSince = 153;//July
    			break;
    		case 6:
    			monthDaysSince = 184;//June
    			break;
    		case 7:
    			monthDaysSince = 214;//May
    			break;
    		case 8:
    			monthDaysSince = 245;//April
    			break;
    		case 9:
    			monthDaysSince = 275;//March
    			break;
    		case 10:
    			monthDaysSince = 306;//February
    			break;
    		case 11:
    			monthDaysSince = 334;//January
    		}
    		String day = null;
    		int yearDaysSince = yearsSince * 365; 
    		[B]if (leapYearsSince == 0 && birthYear % 4 == 0 && birthMonth <=2) {
    			leapYearsSince++;
    		}[/B]
    		int daysSinceBirth = daysSince + leapYearsSince + monthDaysSince + yearDaysSince;
    		int daysFromMonday = daysSinceBirth % 7;
    		switch (daysFromMonday) {
    		case 0:
    			day = "Monday";
    			break;
    		case 1:
    			day = "Sunday";
    			break;
    		case 2:
    			day = "Saturday";
    			break;
    		case 3:
    			day = "Friday";
    			break;
    		case 4:
    			day = "Thursday";
    			break;
    		case 5:
    			day = "Wednesday";
    			break;
    		case 6:
    			day = "Tuesday";
    			break;
    		}
    		out.println("You were born on a " + day + ".");
    		scanned.close();
    	}
     
    }

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Problems with "if " statements in terms of leap years

    can you explain this line?
    when I start to input birthdays greater than about a year old, the program is off by about a day.
    Whatever you are, be a good one

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Problems with "if " statements in terms of leap years

    I'm having a bit of trouble following how you are calculating the birthday-of-the-week. Could you explain it (in English rather than code)? Perhaps giving an example for a specific birthday: 1 Jan, 1970 or whatever.

Similar Threads

  1. Replies: 4
    Last Post: July 18th, 2014, 02:04 AM
  2. Replies: 1
    Last Post: July 16th, 2014, 04:16 AM
  3. Replies: 2
    Last Post: May 22nd, 2014, 01:17 PM
  4. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  5. Problems with a "if-then and if-then-else" statements
    By jjd712 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 7th, 2012, 12:31 AM