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

Thread: This code is not giving correct values

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

    Default This code is not giving correct values

    I am a beginning self taught programmer. I was thinking this would be a good exercise to get used to program control statements. This is an attempt to write a program that returns the number of days between two dates. Sadly, it give incorrect values and after hours of trying to figure out what is wrong, I was hoping someone would help me. I will accept any form of help and criticism to help me improve. Thank you




    public class DaysBetweenDates {
        public static boolean isLeapyear(int year) {//This method will determine if a year is a leap year or not
            boolean isTrue=false;
            if(year%4==0 && year%100!=0) isTrue=true;
            else if(year%400==0)         isTrue=true;
            return isTrue;
        }
        public static int computeDaysLeftInYear(int year1,int month1, int day1) {//This method is supposed to compute the days after the first date in 
                                                                                //the first year
            int daysInYearOne=0;
            switch(month1) {//The purpose of this switch is to get the day remaining in the first month and store them in the variable daysInYearOne
                case 1: 
                    daysInYearOne=31-day1;
                    break;
                case 2: 
                    if(DaysBetweenDates.isLeapyear(year1)) daysInYearOne=29-day1;
                    else daysInYearOne=28-day1;
                    break;
                case 3: 
                    daysInYearOne=31-day1;
                    break;
                case 4: 
                    daysInYearOne=30-day1;
                    break;
                case 5: 
                    daysInYearOne=31-day1;
                    break;
                case 6: 
                    daysInYearOne=30-day1;
                    break;
                case 7: 
                    daysInYearOne=31-day1;
                    break;
                case 8: 
                    daysInYearOne=31-day1;
                    break;
                case 9: 
                    daysInYearOne=30-day1;
                    break;
                case 10: 
                    daysInYearOne=31-day1;
                    break;
                case 11: 
                    daysInYearOne=30-day1;
                    break;
                default: 
                    daysInYearOne=31-day1;
     
     
            }
     
            switch(month1+1) {//This switch is supposed to get the values of the days in the month for the rest of year one stored inside the variable 
                case 2:       //daysInYearOne
                        if(DaysBetweenDates.isLeapyear(year1)) daysInYearOne+=29;
                        else daysInYearOne+=28;
                case 3:
                        daysInYearOne+=31;
                case 4:
                        daysInYearOne+=30;
                case 5:
                        daysInYearOne+=31;
                case 6:
                        daysInYearOne+=30;
                case 7: 
                        daysInYearOne+=31;
                case 8:
                        daysInYearOne+=31;
                case 9:
                        daysInYearOne+=30;
                case 10:
                        daysInYearOne+=31;
                case 11:
                        daysInYearOne+=30;
                default:
                        daysInYearOne+=31;
     
            }
            return daysInYearOne;
        }
     
           public static int computeDaysFromYearsInbetween(int year1, int year2) {//This method is supposed to find the days in the whole years that lie 
               int daysInbetweenYears=0;                                          // between the partial years 
               for(int i=year1+1;i<=year2-1;i++) {
                   if(DaysBetweenDates.isLeapyear(i)) daysInbetweenYears+=366;
                   else daysInbetweenYears+=365;
               }
     
               return daysInbetweenYears;
           }
     
           public static int computeDaysBeforeDate(int year2, int month2, int day2) {//This method is supposed to find the days in the last year by using 
               int daysInYearTwo=0;                                                 //the first method. I was trying to follow the never repeat yourself principle
               if(DaysBetweenDates.isLeapyear(year2)) daysInYearTwo=366-DaysBetweenDates.computeDaysLeftInYear(year2,month2,day2);
               else                                   daysInYearTwo=365-DaysBetweenDates.computeDaysLeftInYear(year2,month2,day2);
               return daysInYearTwo; 
           }
     
           public static void main(String[] args) {
               int v1=DaysBetweenDates.computeDaysLeftInYear(2012,1,1);
               int v2=DaysBetweenDates.computeDaysFromYearsInbetween(2012,2013);
               int v3=DaysBetweenDates.computeDaysBeforeDate(2013,12,31);
     
               System.out.println( v1+v2+v3);
           }
     
     
    }


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: This code is not giving correct values

    the reason you have bugs is because of wrong computation.
    your method public static int computeDaysLeftInYear(int year1,int month1, int day1) actually works without error if and only it is used by getting the days left in a year. but when you incorporate this method with method computeDaysFromYearsInbetween, it does a wrong computation. Since you want some advice, I'll give you some advice.

    I debug your code and trouble some bugs, this is what i noticed:
    your first switch statement in computeDaysLeftInYear method is useless (if incorporate with method computeDaysFromYearsInbetween)
    it would be better to remove that switch.
    But how would you get the number of days to be subtracted?
    just subtract the day1 value to your daysInYearOne variable which have initial value of zero.
    int daysInYearOne = 0 - day1;
    therefore you will have a negative value or zero. then add it to the number of days a month has to get the remaining days.
    exmaple: January 30 will make your daysInYearOne = -30 then add 31 (January has 31 days) it will make your daysInYearOne = 1 in the second switch statement if you follow my advice below


    then in the second switch statement, just add another case, case 1 for first month of the year.
    case 1:
    daysInYearOne += 31;


    then remove the additional value to your switch
    switch (month1 + 1) should be switch (month1)
    the other bug you have is in method computeDaysBeforeDate.
    actually you just have a wrong condition in your loop.
    for(int i=year1+1;i<=year2-1;i++) {
    if(DaysBetweenDates.isLeapyear(i)) daysInbetweenYears+=366;
    else daysInbetweenYears+=365;
    }

    review the condition and initialization.

    Actually, for me, there is a better way of doing that than using switch statement, bu that is fine.

    to make your activity more challenging, try to add some verification and error messages, for example getting a month that is higher than 12, or date that is beyond 31 or beyond 28 for February.
    Last edited by dicdic; March 24th, 2014 at 09:36 PM.

Similar Threads

  1. [SOLVED] Code is not giving the desired output
    By Tree_Bek in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2013, 07:27 PM
  2. Replies: 4
    Last Post: March 6th, 2013, 06:39 AM
  3. For loop not giving the correct output
    By Kattracks32 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 28th, 2013, 03:41 AM
  4. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 08:30 PM
  5. Double to string giving extra values
    By sinsand in forum Collections and Generics
    Replies: 1
    Last Post: August 23rd, 2011, 05:57 AM