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

Thread: if else statements

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default if else statements

    I need to be able to input two numbers, corresponding to a month and year, and have it return the number of days in that month. It all works except for when it gets to february and whether its a leap year or not. Currently it outputs 29, no matter what the year. I think it's something to do with the way the if and else statements are contained in each other, but not sure. Only recently started learning java so. Perhaps someone could give me a hand...

    The isACenturyYear(year) and isDivisibleBy(year,400) are working methods already set above.

        public int daysInMonth(int month, int year)
        {
            int daysInMonth ;
            daysInMonth = 0 ;
            boolean leap ;
            leap = false ;
            if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                daysInMonth = 31 ;
            } else {
                if (month == 4 || month == 6 || month == 9 || month == 11) {
                    daysInMonth = 30 ;
                } else {
                    if(isACenturyYear(year) || month == 2) {
                        leap = isDivisibleBy(year,400) ;
                    } else {
                        leap = isDivisibleBy(year,4) ;
                    }
                    daysInMonth = 29 ;
                    if (isACenturyYear(year) || month == 2) {
                        leap =! isDivisibleBy(year,400) ;
                    } else {
                        leap =! isDivisibleBy(year,4) ;
                    }
                    daysInMonth = 28 ;
                }
            }
            return daysInMonth ;
        }


  2. #2
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: if else statements

    After you assign leap to either true or false, where in your code are you using that value to make decisions? Fix that, and the solution should be obvious

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: if else statements

    Quote Originally Posted by DavidFongs View Post
    After you assign leap to either true or false, where in your code are you using that value to make decisions? Fix that, and the solution should be obvious
    I'm not overly sure to be honest :/

    Here's the methods that were already set, I'm just not sure how to use them...

        public boolean isDivisibleBy(int x, int y)
        {
            boolean answer = false ;
            if(x % y == 0) {
                answer = true ;
            } 
            return answer ;
        }
     
        public boolean isACenturyYear(int year)
        {
            boolean answer = false ;
            if(year % 100 == 0) {
                answer = true ;
            }
            return answer ;
        }
     
        public boolean isLeapYear(int year)
        {
            boolean answer ;
            if(isACenturyYear(year)) {
                answer = isDivisibleBy(year,400) ;
            } else {
                answer = isDivisibleBy(year,4) ;
            }
            return answer ;
        }

    I assumed it would check that if the value of leap is equal to that of isDivisibleBy(year,400) or isDivisibleBy(year,400), it would set the value for daysInMonth, otherwise would set different value.

    Also after seeing, I could probably replace some of that code with just the isLeapYear method yes?

  4. #4
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: if else statements

    You are making this overly complicated. If the month is february, set daysInMonth to 28, then call isLeapYear, and if that is true, add one

  5. The Following User Says Thank You to DavidFongs For This Useful Post:

    mozyman (October 24th, 2010)

  6. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: if else statements

    Ahh finally got it to work wasn't overly sure of how to call isLeapYear, got it through a bit of trial and error though.

    Thanks for your help.
    I dare say i'll be on here again sometime.

Similar Threads

  1. If statements
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 16th, 2010, 10:09 AM
  2. not sure what im doing wrong with these if else statements
    By yrvd86 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2010, 08:32 PM
  3. continue statements
    By monroe in forum Java Applets
    Replies: 1
    Last Post: March 20th, 2010, 06:26 PM
  4. Unreachable statements
    By Marcus in forum Loops & Control Statements
    Replies: 7
    Last Post: December 10th, 2009, 02:02 PM
  5. Need help with While and For Statements
    By duckman in forum Loops & Control Statements
    Replies: 2
    Last Post: October 20th, 2009, 08:42 PM