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;
}
}