Ok guys, firstly i'm sorry for the long code. Don't worry, it is only a small problem related to a small part of the code in the main class. I've highlighted the .area

I have a problem here. The first piece of code is my main class. I call a method from another class that helps me display tomorrows date. The second piece of code shows the class which I am calling. This seems to be working grand.

The next thing I have to do is to prompt the user to enter a number of days and the answer should show what date it will be in X days time as prompted by user.

In the main class, I have prompted the user to enter dates. However, when the user enters say 47 days, the date 47/1/2001 comes up. I can't see why this is happening as I am calling the method again and the method worked fine for the first part but it just doesn't seem to work for the next part. Any ideas?

import javax.swing.*;

class newTomorrow {
    public static void main(String[] args){

        int answer;

        newDate rob = new newDate(31,12,2000);

        System.out.println("The todays date is " + rob.day + "/" + rob.month + "/"
                + rob.year + "\n"); // print todays date.
        
        rob.tomorrow();

        System.out.println("The tomorrows date is " + rob.day + "/" + rob.month + "/"
                + rob.year + "\n"); // print tomorrows date.

        newDate future = new newDate(31,12,2000);
        
    [B]String input1 = JOptionPane.showInputDialog("Please enter number of days" +
            " to display future date:"); // prompt user to enter amount of days.
    answer = Integer.parseInt(input1);
    
    future.day = (future.day - 1) + answer ;// minus the day added tomorrows method and
                                            // add days prompted by user
    future.tomorrow();

    System.out.println("The date in " + answer + " days time will be " + future.day
            + "/" + future.month + "/" + future.year);
    }[/B]

}
class newDate {

    public newDate() {
        day = 1;
        month = 1;
        year = 1970;
    }

    public newDate(int _day, int _month, int _year) {
        day = _day;
        month = _month;
        year = _year;
    }
    int day;
    int month;
    int year;

    public newDate tomorrow() {

        this.day++;

        if(this.day > 31 && month == 1 ){
            this.day = this.day - 31;
            month++;
        }else
            if(month == 2 && year % 4 == 0 && this.day > 29){
                this.day = this.day - 29;
                month++;
            }else
                if(month == 2 && year % 4 != 0 && this.day > 28){
                    this.day = this.day - 28;
                    month++;
                }else
                    if(this.day > 31 && month == 3){
                        this.day = this.day - 31;
                        month++;
                    }else
                        if(this.day > 30 && month == 4){
                            this.day = this.day - 30;
                            month++;
                        }else
                            if(this.day > 31 && month == 5){
                                this.day = this.day - 31;
                                month++;
                            }else
                                if(this.day > 30 && month == 6){
                                    this.day = this.day - 30;
                                    month++;
               }else
                   if(this.day > 31 && month == 7) {
                       this.day = this.day - 31;
                       month++;
                   }else
                       if(this.day > 31 && month == 8){
                           this.day = this.day - 31;
                           month++;
                       }else
                           if(this.day > 30 && month == 9){
                               this.day = this.day - 30;
                               month++;
                           }else
                               if(this.day > 31 && month == 10){
                                   this.day = this.day - 31;
                                   month++;
                               }else
                                   if(this.day > 30 && month == 11){
                                       this.day = this.day - 30;
                                       month++;
                                   }else
                                       if(this.day > 31 && month == 12){
                                           this.day = this.day - 31;
                                           month++;
                                           month = month - 12;
                                           year++;
                                       }
        
        return this;

    }
    
}

Hey, I looked at your code and I'm really interested in what you've done with the tomorrow method… but that's another story.

I made the following alterations to the main method, and I think I've figured out what's going badly.

import javax.swing.*;

class newTomorrow {
  public static void main(String[] args){
    
    int answer;
    
    newDate rob = new newDate(5,12,2000); // I just changed the date.
    
    System.out.println("The todays date is " + rob.day + "/" + rob.month + "/"
                         + rob.year + "\n"); // print todays date.
    
    rob.tomorrow();
    
    System.out.println("The tomorrows date is " + rob.day + "/" + rob.month + "/"
                         + rob.year + "\n"); // print tomorrows date.
    
   // newDate future = new newDate(31,12,2000); This guy was not really needed.
    
    String input1 = JOptionPane.showInputDialog("Please enter number of days" +
                                                " to display rob date:"); // prompt user to enter amount of days.
    answer = Integer.parseInt(input1);
    
    rob.day = (rob.day - 1) + answer ;// minus the day added tomorrows method and
    // add days prompted by user
    rob.tomorrow();
    
    System.out.println("The date in " + answer + " days time will be " + rob.day
                         + "/" + rob.month + "/" + rob.year);
  }
  
}

Ok, so when you invoke the tomorrow method a second time on the rob object, rob.day is equal to 75. When it reaches the collection of if/else statements you have set up it is 76. The only if/else condition that gets evaluated is the one requiring the month to be December. So 31 is subtracted from 76 and the date printed to the screen is 45/1/2001. Do you see what's going on? You have nothing in place to compensate for a difference larger than the number of days in the following month. So what ever the difference is, that is what the day is set to. (i.e. if this.day - 31 evaluated to 300 the date would print out as 300/1/2001)

I'm not sure how you might go about fixing this but I'll think about it and get back to you. Right now I'm more interested in the method declaration of tomorrow().

public newDate tommorow(){/* A ton of code */; return this;}

So the return type is an instance of your newDate class, but where I'm intrigued is the return statement. What exactly are you returning? The object on which the method was invoked or is it just a reference to that object? I've never seen that done before and I'm curious that's all.

Thanks!

Edited 6 Years Ago by rue64ja: n/a

Hey Rue,

With the method, basically all thats happening is that its returning this.day++. The ton of code is supposed to be the restrictions once the day id added i.e if month = january and day is greater than 31, day = day -31 so it goes to 1, and month++. I'm sure it's a very long way of doing it. What do you think?

Also, I'm not sure why the date would be 75.

Edited 6 Years Ago by Bobon: n/a

I think I know what you mean. Is it that in the class I have something there if the date goes about 31, but nothing if it goes about 62 say?

rob.day = (rob.day - 1) + answer ;

What I was trying to point out starts with this line of code. Lets say that rob.day returns 3. Which means it is the third day of the month. What this line of code does is subtract 1 from that 3, and then adds some other number that the user enters to that difference. What ever the result of that calculation, the value is then assigned to rob.day.

Suppose the user enters 80, then your line of code would look like this

rob.day = (3 - 1) + 80;

So rob.day now equals 82. You then invoke the tomorrow() method on the rob object, and 1 is immediately added to the day… rob.day is now 83. What decides which if statement executes would depend on what month it is because rob.day is much larger than any of the restrictions specified, lets just say it was December. Then the code which executes is this…

if(this.day > 31 && month == 12){
                                           this.day = this.day - 31;
                                           month++;
                                           month = month - 12;
                                           year++;
                                       }

When this block of code executes this.day gets assigned a value of 83 - 31, which is 52. You have no code in place that deals with something like this. If a user wishes to know what the date would be in three or four months, and enters 100 days lets say, your program doesn't know how to process that. It can only predict the correct date so long as the user enters a number of days that is within the month rob.month, other wise you get that weird out put telling you there are 47 days in December.

Like I said in the other post, I'm not sure how to fix it right away. But knowing what the problem is… is essential to the solution process right? If you're not in any particular rush, I'll think about it and get back to you. Or maybe you might get to it before me…

Rue, I understand you.

Whats actually happening for me is, say I display todays and tomorrows date. Todays date is 3/1/2010, it's displays this and tomorrows date fine. When i add 30 to it, it correctly displays 2/1/2010. However, when i get to after february it starts to display crazy dates like you said.

Yeah I'd definitely appreciate it if you could come up with something. Thanks rue.

Rue, I actually got it working. I can't believe it. The only thing is I can't get it to keep looping once the date goes into a new year. I'll show you what i did.

import javax.swing.*;

class Tomorrow {
    public static void main(String[] args){

        int answer;

        Date rob = new Date(2,1,2000);

        System.out.println("The todays date is " + rob.day + "/" + rob.month + "/"
                + rob.year + "\n"); // print todays date.

        rob.tomorrow();

        System.out.println("The tomorrows date is " + rob.day + "/" + rob.month + "/"
                + rob.year + "\n"); // print tomorrows date.

        String input1 = JOptionPane.showInputDialog("Please enter number of days" +
                " to display future date:"); // prompt user to enter amount of days.
        answer = Integer.parseInt(input1);

        rob.day = (rob.day - 1) + answer ;// minus the day added tomorrows method and
        rob.tomorrow();                 // add days prompted by user

        [B]int[] lastDay = {31,28,31,30,31,30,31,31,30,31,30,31};

        while(rob.day > lastDay[rob.month-1]){
            rob.day = rob.day - lastDay[rob.month-1];
            rob.month++;
        }[/B]


        System.out.println("The date in " + answer + " days time will be " +
                rob.day + "/" + rob.month + "/" + rob.year);
        }

}

I added an array and a while loop in. Now this allows me to enter days up until the 31/12/2000 say. As soon as the number of days go into a new year, the array out of bounds exception comes up. Any ideas how to fix that.

My god, i'm very relieved to have that bit done.

This article has been dead for over six months. Start a new discussion instead.