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

Thread: If Statement not working

  1. #1
    Junior Member
    Join Date
    May 2014
    Location
    Toronto
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default If Statement not working

    Hi I'm new to programming so I'm having a problem where an if statement isn't working. In the Person class boolean olderThan ignores my if statement so it always returns false and I just don't get why that's happening.

    import java.util.Calendar;
    public class Person {
        private String name;
        private MyDate birthday;
     
        public Person(String name, int pp, int kk, int vv) {
            this.name = name;
            this.birthday = new MyDate(pp, kk, vv);
        }
     
     
        public int age() {
            MyDate today = new MyDate ( Calendar.getInstance().get(Calendar.DATE) ,
                    Calendar.getInstance().get(Calendar.MONTH) + 1 ,
                    Calendar.getInstance().get(Calendar.YEAR));
            return today.differenceInYears( this.birthday );
     
        }
     
        public boolean olderThan(Person compared) {
            if ( this.age() > compared.age()){
                return true;
            }
            return false;
        }
     
        public String getName() {
            return this.name;
        }
     
        public String toString() {
            return this.name + ", born " + this.birthday;
        }
     
     
    }

     
    public class MyDate {
     
        private int day;
        private int month;
        private int year;
     
        public MyDate(int pv, int kk, int vv) {
            this.day = pv;
            this.month = kk;
            this.year = vv;
        }
     
        public String toString() {
            return this.day + "." + this.month + "." + this.year;
        }
     
        public boolean earlier(MyDate compared) {
            if (this.year < compared.year) {
                return true;
            }
     
            if (this.year == compared.year && this.month < compared.month) {
                return true;
            }
     
            if (this.year == compared.year && this.month == compared.month
                    && this.day < compared.day) {
                return true;
            }
     
            return false;
        }
     
     
        public int differenceInYears (MyDate comparedDate){
            return Math.abs((((this.year*365)+(this.month*30)+this.day)-((comparedDate.year
                    *365)+(comparedDate.month*30)+comparedDate.day))/365);
     
        }
     
     
    }
    Last edited by Szpak; May 5th, 2014 at 05:33 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: If Statement not working

    olderThan ignores my if statement so it always returns false
    Try debugging the code by adding a println statement that prints out the values of both variables used in the if statement so you can see what the computer sees when the code executes.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Szpak (May 5th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: If Statement not working

    Comments in your code to describe what the different if statements are supposed to accomplish would be helpful. Since only false is returned, none of the if statements are successful. Why? We don't know why from what you've given us other than because none of the if conditions resolve to true.

    You might also think about a more elegant way to simplify a chain of if statements that essentially say:
    if ( true )
    {
        return true;
    }
     
    if ( true )
    {
        return true;
    }
     
    if ( true )
    {
        return true;
    }
     
    return false;

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

    Szpak (May 5th, 2014)

  6. #4
    Junior Member
    Join Date
    May 2014
    Location
    Toronto
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: If Statement not working

    Thanks turns out it was working perfectly and i put the wrong values in the if statement for what I wanted.

    --- Update ---

    Also in response to Greg, I'm assuming you're talking about public boolean earlier. It's the pre-made code for the class I'm taking and the testing program sometimes doesn't give you the marks if certain elements aren't how they were given.

Similar Threads

  1. If else statement not working.
    By DarkestLord in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 13th, 2014, 01:17 PM
  2. If/else statement working incorrectly...
    By jcfor3ver in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 6th, 2013, 12:26 AM
  3. if else if elese if.... statement not working
    By ahans in forum Loops & Control Statements
    Replies: 4
    Last Post: October 25th, 2013, 01:54 PM
  4. Replies: 2
    Last Post: June 25th, 2013, 06:33 AM
  5. if else statement not working properly
    By tina G in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 29th, 2010, 08:04 AM