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

Thread: whats wrong? help to newbie

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default whats wrong? help to newbie

    hi all,
    can any one tell me what's wrong?
    i'm triyng to get next date from object that I created and still getting the same date

    public Date nextDay()
       { int newDay=this.day, newMonth=this.month, newYear=this.year;
            if ((this.month == 1) || (this.month == 3) || (this.month == 5) || (this.month == 7) || (this.month == 8) || (this.month == 10)){
               if (this.day < 31)
               newDay = +this.day;
               if (this.day == 31)
               newDay = 01;
               newMonth = +this.month;
            }
            if ((this.month == 4) || (this.month == 6) || (this.month == 9) || (this.month == 11)){
              if (this.day < 30)
              newDay = +this.day;
              if (this.day == 30)
              newDay = 01;
              newMonth = +this.month;
            }
            if (this.month == 2){
              if (this.day < 28)
               newDay = +this.day;
              if (this.day == 28)
               newDay = 01;
               newMonth = +this.month;
              if (((this.year%4 == 0) && (this.year%100!=0) || (this.year%400==0)) && (this.day==28))
              newDay = +this.day;
            }
            if (this.month == 12){
               if (this.day < 31)
               newDay = +this.day;
               if (this.day == 31)
               newYear = +this.year;
               newMonth = 01;
               newDay = 01;
            }   
         return new Date(newDay, newMonth, newYear);
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: whats wrong? help to newbie

    still getting the same date
    Please explain.
    Add some printlns to your program to show the problem.
    Print out the values of all the variables that you are using to construct the Date object.

    How can it be the same date? You create a new instance of the Date class.

  3. #3
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: whats wrong? help to newbie

    +this.day
    will never change the day. I provided a link below explaining more.
    Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

  4. The Following User Says Thank You to Tjstretch For This Useful Post:

    ribhoo (November 28th, 2011)

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

    Default Re: whats wrong? help to newbie

    here is the whole code:
     
    public class Date
    {
     
     
        private int day;
        private int month;
        private int year;
     
     
        public Date( int day, int month, int year)
        {
            this.day=day;
            this.month=month;
            this.year=year;
         }
     
     
     
        public Date(Date d )
        {  
            this.day=d.day;
            this.month=d.month;
            this.year=d.year;
        }
     
        public int getDay()
        {
            return this.day;
        }
     
         public int getMonth()
        {
            return this.month;
        }
     
        public int getYear()
        {
            return this.year;
        }
     
         public void setDay(int day)
        {
            this.day=day;
        }
     
         public void setMonth(int month)
        {
            this.month=month;
        }
     
         void setYear(int year)
        {
            this.year=year;
        }
     
        public String toString()
        {
            String stDay=""+this.day;
            if (this.day<10)
                stDay="0"+this.day; 
            String stMon=""+this.month;
            if (this.month<10)
                 stMon="0"+this.month;
            return ("date: "+stDay+"/"+stMon+"/"+this.year);
        }
     
       public Date nextDay()
       { int newDay=this.day, newMonth=this.month, newYear=this.year;
            if ((this.month == 1) || (this.month == 3) || (this.month == 5) || (this.month == 7) || (this.month == 8) || (this.month == 10)){
               if (newDay < 31)
               newDay = +this.day;
               else
               newDay = 01;
               newMonth = +this.month;
            }
            if ((this.month == 4) || (this.month == 6) || (this.month == 9) || (this.month == 11)){
              if (this.day < 30)
              newDay = +this.day;
              if (this.day == 30)
              newDay = 01;
              newMonth = +this.month;
            }
            if (this.month == 2){
              if (this.day < 28)
               newDay = +this.day;
              if (this.day == 28)
               newDay = 01;
               newMonth = +this.month;
              if (((this.year%4 == 0) && (this.year%100!=0) || (this.year%400==0)) && (this.day==28))
              newDay = +this.day;
            }
            if (this.month == 12){
               if (this.day < 31)
               newDay = +this.day;
               if (this.day == 31)
               newYear = +this.year;
               newMonth = 01;
               newDay = 01;
            }   
         return new Date(newDay, newMonth, newYear);
        }

    I'm using bluej so I can create an object with parameters and trying to do nextDay, getting the same date that I set in object before

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

    Default Re: whats wrong? help to newbie

    Quote Originally Posted by Tjstretch View Post
    +this.day
    will never change the day. I provided a link below explaining more.
    Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
    thanks, now when I'm changed the code to
    ++this.day
    I'm getting +2
    tried with date 3 1 85 and got 5 3 85

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: whats wrong? help to newbie

    Add some printlns to your program to show the problem.
    Print out the values of all the variables that you are using to show their values before and after.
    Add some comments describing what is wrong with the printed output.

    Also the java SE has a class named: Date.
    It would be less confusing to anyone reading your code if you chose a unique name for your class.

  8. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: whats wrong? help to newbie

    ok, so I found the problem now it's work
    here is the code:
    { int newDay=this.day, newMonth=this.month, newYear=this.year;
            if ((this.month == 1) || (this.month == 3) || (this.month == 5) || (this.month == 7) || (this.month == 8) || (this.month == 10)){
               if (this.day < 31){
               newDay = ++this.day;
            }
               if (this.day == 31){
               newDay = 01;
               newMonth = ++this.month;
            }
            }

Similar Threads

  1. Whats wrong here?
    By Java Sucks in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 8th, 2011, 08:33 PM
  2. whats wrong?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2011, 02:35 PM
  3. could you tell me whats wrong....
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 6th, 2010, 04:35 PM
  4. help whats wrong
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 3rd, 2009, 01:41 AM
  5. [SOLVED] whats wrong with my IDE
    By chronoz13 in forum Java IDEs
    Replies: 2
    Last Post: August 27th, 2009, 06:34 AM